<el-tree :data="treedata" :props="defaultProps"
@node-click="handleNodeClick"
node-key="category_id" :highlight-current="true" ref="myTree"
:current-node-key="currentNodeKey"
:default-expanded-keys="currentNodeKeyarr">
</el-tree>
treedata: [],
defaultProps: {
children: 'children',
label: 'category_name'
},
article_category_id: '', // 当前文章所属分组id
currentNodeKey: '', // m默认选中节点树 "ca47b82a-3c3-fde-a7bc-27cef5a9ec101"
currentNodeKeyarr: [], // 测试用例"ca47b82a-3c3-fde-a7bc-27cef5a9ec75e","ca47b82a-3c3-fde-a7bc-27cef5a9ec100"
```
// 递归算法匹配id路径
findPathById(treeDataArray, targetId, currentPath = []){
// console.log('this.currentNo12deKeyarr12311', treeData.children)
for (const treeData of treeDataArray) {
if (treeData.category_id === targetId) {
return [...currentPath, treeData.category_id];
}
if (treeData.children && treeData.children.length > 0) {
const path = this.findPathById(treeData.children, targetId, [...currentPath, treeData.category_id]);
if (path) {
return path;
}
}
}
return null;
},
```
//核心赋值代码
const result = this.findPathById(resdata, targetId)
this.currentNodeKeyarr = result
//树状图高亮
setTimeout(() => {
this.$nextTick(function () {
this.$nextTick(() => {
this.$refs.myTree.setCurrentKey(this.currentNodeKey);
});
});
},1000)
因篇幅问题不能全部显示,请点此查看更多更全内容