How to expand and collapse all element trees
1. Expand all
id
Just put each parent node's defaultExpendKeys
into the array
<el-tree
class="tree-transparent"
ref="projectTree"
:draggable="false"
:default-expanded-keys="defaultExpandedKeys"
@node-click="treeNodeClicked"
:check-on-click-node="true"
:data="treeData"
:highlight-current="true"
:default-expand-all="false"
:expand-on-click-node="false"
node-key="id">
For example, my tree structure has three levels, so I need to id
put all of the first two levels defaultExpandKeys
into
// TREE OPERATION
expandAll(){
this.defaultExpandedKeys = []
this.treeData.forEach(type => {
this.defaultExpandedKeys.push(type.id)
type.children.forEach(channel => {
this.defaultExpandedKeys.push(channel.id)
})
})
},
It all unfolded.
2. Fold all
element-tree
There is no way to fold them all, but it can be worked around.
Just reload the data of tree
, its default form is folded.
For example this tree
data is treeData
, you can:
collapseAll(){
this.getProjectInfo() // 重新获取树的数据
this.defaultExpandedKeys = []
},