vue Element-ui表格實(shí)現(xiàn)樹形結(jié)構(gòu)表格
本文實(shí)例為大家分享了Element-ui表格實(shí)現(xiàn)樹形結(jié)構(gòu)表格的具體代碼,供大家參考,具體內(nèi)容如下
前端效果展示:

在el-table中,支持樹類型的數(shù)據(jù)的顯示。當(dāng) row 中包含 children 字段時,被視為樹形數(shù)據(jù)。渲染樹形數(shù)據(jù)時,必須要指定 row-key。支持子節(jié)點(diǎn)數(shù)據(jù)異步加載。
通過指定 row 中的 hasChildren 字段來指定哪些行是包含子節(jié)點(diǎn)。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 實(shí)現(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(`當(dāng)前頁: ${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)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
基于Vue3自定義實(shí)現(xiàn)圖片翻轉(zhuǎn)預(yù)覽功能
這篇文章主要為大家詳細(xì)介紹了如何基于Vue3自定義實(shí)現(xiàn)簡單的圖片翻轉(zhuǎn)預(yù)覽功能,文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價值,有需要的小伙伴可以參考一下2023-10-10
Vue數(shù)據(jù)驅(qū)動模擬實(shí)現(xiàn)4
這篇文章主要介紹了Vue數(shù)據(jù)驅(qū)動模擬實(shí)現(xiàn)的相關(guān)資料,介紹了Array的變異方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-01-01
查找Vue中下標(biāo)的操作(some和findindex)
這篇文章主要介紹了查找Vue中下標(biāo)的操作(some和findindex),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-08-08
axios+vue請求時攜帶cookie的方法實(shí)例
做項(xiàng)目時遇到一個需求,后端需要在接口請求時,對用戶登陸狀態(tài)進(jìn)行判斷,需要在請求時攜帶Cookie,下面這篇文章主要給大家介紹了關(guān)于axios+vue請求時攜帶cookie的相關(guān)資料,需要的朋友可以參考下2022-09-09
關(guān)于delete和Vue.delete的區(qū)別及說明
這篇文章主要介紹了關(guān)于delete和Vue.delete的區(qū)別及說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-10-10
vue3.0使用vue-pdf-embed在線預(yù)覽pdf 控制頁碼顯示范圍不生效問題解決
這篇文章主要介紹了vue3.0使用vue-pdf-embed在線預(yù)覽pdf 控制頁碼顯示范圍不生效問題的問題及解決方法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧2023-01-01
Element中Upload組件上傳功能實(shí)現(xiàn)(圖片和文件的默認(rèn)上傳及自定義上傳)
這篇文章主要介紹了Element中Upload組件上傳功能實(shí)現(xiàn)包括圖片和文件的默認(rèn)上傳及自定義上傳,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧2024-01-01

