关于tree数之间的父子关联/全选/取消全选/展开节点/关闭展开之间的操作
多的不说 直接上代码
<el-select v-model="value" placeholder="树操作" @change="treeOperation" style="width:10%!important">
<el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value" />
</el-select>
<el-tree :data="permissions" ref="tree"
show-checkbox
node-key="id"
:default-expanded-keys="[0]"
:check-strictly="checkStrictly"
:default-checked-keys="defaultCheckedKeys"
:props="defaultProps">
</el-tree>
data(){
return{
permissions:[],
value:'',
options: [{
value: '1',
label: '父子关联'
}, {
value: '2',
label: '取消关联'
}, {
value: '3',
label: '全部勾选'
}, {
value: '4',
label: '取消勾选'
}, {
value: '5',
label: '展开所有'
}, {
value: '6',
label: '关闭所有'
}],
}
}
treeOperation() {
if (this.value=== '1') {
this.checkStrictly = false
} else if (this.value=== '2') {
this.checkStrictly = true
} else if (this.value=== '3') {
this.checkStrictly = false
this.$nextTick(() => {
this.$refs.tree.setCheckedNodes(this.permissions)
})
} else if (this.value=== '4') {
this.checkStrictly = false
this.$nextTick(() => {
this.$refs.tree.setCheckedNodes([])
})
} else if (this.value=== '5') {
for (let i = 0; i < this.$refs.tree.store._getAllNodes().length; i++) {
this.$refs.tree.store._getAllNodes()[i].expanded = true
}
}else if (this.value=== '6') {
for (let i = 0; i < this.$refs.tree.store._getAllNodes().length; i++) {
this.$refs.tree.store._getAllNodes()[i].expanded = false
}
}
},
//获取选中和半选中状态下的数据
this.$refs.tree.getCheckedKeys().concat(this.$refs.tree.getHalfCheckedKeys())
如果有帮助到您,希望点个赞再走吧!