<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<style>
</style>
<body>
<div id="app">
<el-button type="primary" @click="handleExpand($event)">一键展开</el-button>
<el-button type="primary" @click="handleExpand(null)">一键收起</el-button>
<el-tree ref="tree" :data="treeData" show-checkbox node-key="id" default-expand-all :props="defaultProps">
</el-tree>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
<link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
<script src="https://unpkg.com/element-ui/lib/index.js"></script>
<script>
var app = new Vue({
el: '#app',
data: {
treeData: [{
id: 1,
label: '一级 1',
children: [{
id: 4,
label: '二级 1-1',
children: [{
id: 9,
label: '三级 1-1-1'
}, {
id: 10,
label: '三级 1-1-2'
}]
}]
}, {
id: 2,
label: '一级 2',
children: [{
id: 5,
label: '二级 2-1'
}, {
id: 6,
label: '二级 2-2'
}]
}],
defaultProps: {
children: 'children',
label: 'label'
}
},
mounted() { },
methods: {
handleExpand(value) {
console.log('value: ', value);
let treeList = this.treeData
this.treeData.map((itemm, index) => {
this.$refs.tree.store.nodesMap[treeList[index].id].expanded = value
this.$refs.tree.root.expanded = value
})
},
}
})
</script>
</body>
</html>
标签:树结构,treeData,一键,label,value,children,element,id From: https://www.cnblogs.com/mysn/p/16772475.html