获取el-tree所有层级的id
2023-07-05 本文已影响0人
泪滴在琴上
点击到树的某一级,官方事件只返回了最后一级的id,如何获取整个点击路径的id
findPath(node, targetId, path = []) {
if (node.dept_id === targetId) {
path.push(node.dept_id);
return true;
}
if (node.children && node.children.length > 0) {
for (let child of node.children) {
if (this.findPath(child, targetId, path)) {
path.push(node.dept_id);
return true;
}
}
}
return false;
},
getPathToRoot(tree, targetId) {
// 辅助函数,递归遍历树形结构
let path = [];
for (let node of tree) {
if (this.findPath(node, targetId, path)) {
break;
}
}
return path.reverse();
},
handleNodeClick(nodeData){
let nodeIds = []; // 存储节点的 id
nodeIds = this.getPathToRoot(this.departmentList, nodeData.dept_id);
},