1. To judge whether to be selected by default according to whether checked is 1 (1 is selected, 2 is unselected). The problem now is that if both checked are 1, only the last one will be executed, and now the desired effect is: as long as checked is 1, it will be selected by default
.<template>
<div>
<el-tree
:data="data2"
show-checkbox
default-expand-all
node-key="checked"
ref="tree"
highlight-current
:props="defaultProps">
</el-tree>
<div class="buttons">
<el-button @click="setCheckedNodes"> node </el-button>
</div>
</div>
</template>
<script>
export default {
methods: {
setCheckedNodes() {
console.log(this.$refs.tree)
this.$refs.tree.setCheckedNodes([{
checked: 1
}]);
}
},
data() {
return {
data2: [{
checked: 1,
label: " 1",
children: [{
checked: 0,
label: " 1-1",
children: [{
checked: 0,
label: " 1-1-1"
}, {
checked: 0,
label: " 1-1-2"
}]
}]
}, {
checked: 1,
label: " 2",
children: [{
checked: 0,
label: " 2-1"
}, {
checked: 0,
label: " 2-2"
}]
}, {
checked: 0,
label: " 3",
children: [{
checked: 0,
label: " 3-1"
}, {
checked: 0,
label: " 3-2"
}]
}],
defaultProps: {
children: "children",
label: "label"
}
};
}
};
</script>