vue Element-ui表格實現(xiàn)樹形結(jié)構(gòu)表格
本文實例為大家分享了Element-ui表格實現(xiàn)樹形結(jié)構(gòu)表格的具體代碼,供大家參考,具體內(nèi)容如下
前端效果展示:
在el-table中,支持樹類型的數(shù)據(jù)的顯示。當 row 中包含 children 字段時,被視為樹形數(shù)據(jù)。渲染樹形數(shù)據(jù)時,必須要指定 row-key。支持子節(jié)點數(shù)據(jù)異步加載。
通過指定 row 中的 hasChildren 字段來指定哪些行是包含子節(jié)點。children 與 hasChildren 都可以通過 tree-props 配置。
row-key="id"和:tree-props="{children: 'children', hasChildren: 'hasChildren'}是必須的。
下面是vue的表格樹:
<!--表格--> <el-row> <el-table :data="tableData" style="width: 100%;" row-key="id" :tree-props="{children: 'children', hasChildren: 'hasChildren'}"> <el-table-column prop="privilegeName" label="權(quán)限名稱" > </el-table-column> <el-table-column prop="privilegeCode" label="權(quán)限編碼" > </el-table-column> <el-table-column prop="privilegeType" label="權(quán)限類別" :formatter="formatPrivilegeType" > </el-table-column> <el-table-column label="操作"> <template slot-scope="scope"> <el-button type="primary" size="mini" @click="toAdd(scope)">新增</el-button> <el-button type="primary" size="mini" @click="toEdit(scope)">編輯</el-button> </template> </el-table-column> </el-table> <br> <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="pagination.pageIndex" :page-sizes="[5, 10, 20, 30, 40]" :page-size=pagination.pageSize layout="total, prev, pager, next" :total=pagination.total> </el-pagination> </el-row>
后端代碼:SpringBoot+MyPlus+MySQL8 實現(xiàn)數(shù)據(jù)結(jié)構(gòu)查詢
前端全部代碼:
<style> </style> <template> <div id="privilege-manager"> <!--頂部菜單欄--> <el-form :inline="true" class="demo-form-inline"> <el-form-item> <el-button class="el-icon-refresh" type="primary" @click="toAdd()">添加 </el-button> </el-form-item> </el-form> <!--表格--> <el-row> <el-table :data="tableData" style="width: 100%;" row-key="id" :tree-props="{children: 'children', hasChildren: 'hasChildren'}"> <el-table-column prop="privilegeName" label="權(quán)限名稱" > </el-table-column> <el-table-column prop="privilegeCode" label="權(quán)限編碼" > </el-table-column> <el-table-column prop="privilegeType" label="權(quán)限類別" :formatter="formatPrivilegeType" > </el-table-column> <el-table-column label="操作"> <template slot-scope="scope"> <el-button type="primary" size="mini" @click="toAdd(scope)">新增</el-button> <el-button type="primary" size="mini" @click="toEdit(scope)">編輯</el-button> </template> </el-table-column> </el-table> <br> <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="pagination.pageIndex" :page-sizes="[5, 10, 20, 30, 40]" :page-size=pagination.pageSize layout="total, prev, pager, next" :total=pagination.total> </el-pagination> </el-row> </div> </template> <script> export default{ name: 'privilege-manager', data () { return { tableData: [], dialogFormEdit: false, dialogFormAdd:false, privilege: { id: '', privilegeName: '', privilegeCode: '', privilegeType: '', pid: '0' }, pagination: { pageIndex: 1, pageSize: 10, total: 0, } } }, methods:{ init () { var self = this this.$axios({ method:'post', url:'/api/baoan/privilege/getPage', data:{"page":this.pagination.pageIndex,"limit":this.pagination.pageSize, "pid": this.privilege.pid}, headers:{ 'Content-Type':'application/json;charset=utf-8' //改這里就好了 } }).then(res => { console.log(res); self.pagination.total = res.data.datas.data.total; self.tableData = res.data.datas.data.records; }) .catch(function (error) { console.log(error) }) }, handleSizeChange(val) { console.log(`每頁 ${val} 條`); this.pagination.pageSize = val; this.pagination.pageIndex = 1; this.init(); }, handleCurrentChange(val) { console.log(`當前頁: ${val}`); this.pagination.pageIndex = val; this.init(); }, // 權(quán)限類別轉(zhuǎn)換 formatPrivilegeType: function( row, column) { if(row.privilegeType === '1'){ return '目錄' } else if(row.privilegeType === '2') { return '菜單' } else if (row.privilegeType === '3') { return '按鈕' } else { return '' } } }, mounted: function () { this.init() } } </script>
總結(jié):
一、注意需要在前端表格里面改的是:
二、后端主要改的是:
(1)視圖層里面加入視圖層集合屬性,注意一定要命名為children,這樣前端才能渲染成樹型結(jié)構(gòu)。
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Vue引入使用localforage改進本地離線存儲方式(突破5M限制)
這篇文章主要介紹了Vue引入使用localforage改進本地離線存儲方式(突破5M限制),具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-03-03vue 點擊其他區(qū)域關(guān)閉自定義div操作
這篇文章主要介紹了vue 點擊其他區(qū)域關(guān)閉自定義div操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-07-07Vue3-新特性defineOptions和defineModel示例詳解
在Vue3.3中新引入了defineOptions宏主要是用來定義Option API的選項,可以用defineOptions定義任意的選項,props、emits、expose、slots除外,本文給大家介紹Vue3-新特性defineOptions和defineModel,感興趣的朋友一起看看吧2023-11-11解決element-ui中下拉菜單子選項click事件不觸發(fā)的問題
今天小編就為大家分享一篇解決element-ui中下拉菜單子選項click事件不觸發(fā)的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-08-08詳解vue 動態(tài)加載并注冊組件且通過 render動態(tài)創(chuàng)建該組件
這篇文章主要介紹了vue 動態(tài)加載并注冊組件且通過 render動態(tài)創(chuàng)建該組件,本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2019-05-05