我是靠谱客的博主 任性大地,这篇文章主要介绍VUE2 Element表格树形结构实现单节点展开,现在分享给大家,希望可以做个参考。

在网上找了很多别人写的发现都不能实现我的需求,最后自己突然想到了一个方法 然后写在这里记录一下 ,方便以后回顾

在el-tabel中需要加上下列

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<el-table ref="tabel" lazy :data="list" :row-key="getRowKeys" :load="load" :expand-row-keys="expands" @expand-change="expandChange" > 需要在data里定义变量 :expands 查找对象的下标 Array.prototype.objectIndex=function(value, lookUpValue){ let num = null try{ this.forEach( (item ,index)=>{ if(item[value] ===lookUpValue){ num = index throw new Error('') } }) }catch{ return num } return -1 }

将下列方法定义在methods里
 

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// 用户点击展开OR收起触发的回调事件 expandChange(row,state) { // row 用户点击收起OR展开时返回的所在行的数据 // state Boolean FALSE OR TRUE // 当用户点击关闭 if(!state){ this.changeDeep(row) return } let index = this.expands.objectIndex('id',row.higherOrg) // Array.prototype.objectIndex自定义方法 返回查找的数据所在的下标 // 判断 是否是子节点 FALSE if(index === -1){ this.expands.forEach( item => { if(item.id === row.id) return //排除当前选中行 this.changeDeep(item,false)// 收起兄弟节点 }) } // 判断 是否是子节点 TRUE if(index !== -1){ this.expands[this.expands.objectIndex('id',row.higherOrg)]?.children.forEach( item => { if(item.id === row.id) return //排除当前选中行 this.changeCode(item,false) // 收起兄弟节点 }) } this.expands.push(row) // 将用户当前选中的行数据保存起来 }, // 返回row的id作为key getRowKeys(row){ return row.id }, // 收起row的方法 close(row,state){ state = state?? false this.$refs.tabel.toggleRowExpansion(row, state) }, // 通过递归 将所有的下级子菜单关闭 changeDeep(row,state){ // 调用关闭方法 this.close(row,state) // 将已经关闭的row 在保存的所有展开的row中删除提高下次查找效率 this.$delete(this.expands,this.expands.objectIndex('id',row.id)) if(row.children?.length){ row.children.forEach( item => { this.changeDeep(item,state) }) } }

最后

以上就是任性大地最近收集整理的关于VUE2 Element表格树形结构实现单节点展开的全部内容,更多相关VUE2内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(46)

评论列表共有 0 条评论

立即
投稿
返回
顶部