<template>
<div class="page">
<el-tree :data="glTree" show-checkbox :check-strictly="true" node-key="id" :default-checked-keys="editid"ref="tree" current-node-key @check="setSelectedNode" :props="defaultProps"></el-tree>
</div>
</template>
export default {
data() {
return {
editArr:[],
NowArr:[],
}
},
methods: {
setSelectedNode(data) {
this.editid.push(data.id)
const node = this.$refs.tree.getCheckedNodes();//获取所有点击的节点
this.NowArr.push(data);//自定义数组存储所有点击节点
if (this.NowArr.length > 1) {//循环所有存储节点
this.editArr.splice(this.editArr.length - 1, 1);
this.editArr.push(data)
this.NowArr = []
this.NowArr.push(data);
} else {
this.editArr.push(data)
}
this.$refs.tree.setCheckedNodes(this.editArr);//设置之前勾选的节点不会因为手动勾选而发生变化
},
ArrBl(arr) {//循环取出后台返回的所有已选择的节点,也可以用于循环多层数组
for (let i = 0; i < arr.length; i++) {
if (arr[i].bindState) {
this.editArr.push(arr[i])
this.editid.push(arr[i].id)//设置树形结构初始化勾选
}
if (arr[i].children && arr[i].children.length > 0) {
this.ArrBl(arr[i].children);
}
}
},
}