✨(component): 处理 tree 组件 n 多问题
This commit is contained in:
34
package/component/src/utils/treeUtil.ts
Normal file
34
package/component/src/utils/treeUtil.ts
Normal file
@@ -0,0 +1,34 @@
|
||||
export function getNode(root: any, id: string) {
|
||||
let resultNode = null;
|
||||
findNode(root, id);
|
||||
function findNode(root: any, id: string){
|
||||
if(!!root) {
|
||||
let type = Object.prototype.toString.call(root);
|
||||
if(type === '[object Object]') {
|
||||
if(root.id && root.id === id) {
|
||||
resultNode = root
|
||||
} else {
|
||||
let node = root.children || null
|
||||
findNode(node, id);
|
||||
}
|
||||
}else if(type === '[object Array]') {
|
||||
let needNode = root.find((i:any) => !!i === true && i.id === id);
|
||||
if(!!needNode) {
|
||||
resultNode = needNode;
|
||||
} else {
|
||||
if(root.length) {
|
||||
root.forEach((item: any)=>{
|
||||
if(item && item.children) {
|
||||
let node = item.children
|
||||
if(node && node.length){
|
||||
findNode(node, id);
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return resultNode;
|
||||
}
|
||||
Reference in New Issue
Block a user