[{id:1,title:node1,parentId:null},{id:2,title:node2,parentId:1},{id:3,title:node3.parentId:null},{id:4,title:node4,parentId:2},{id:5,title:node5,parentId:3}]是上述常见后端返回的数据结构形式。可用以下方法构造树:getParentData(data){letcloneData=JSON.parse(JSON.stringify(data));让parentData=[];for(letparentofcloneData){if(parent.parentId===null||parent.parentId===""){parent.label=parent.title;parent.value=parent.id;让childrenCopy=this.getChildren(parent.id,cloneData);如果(childrenCopy.length>0){parent.children=childrenCopy;}parentData.push(parent);}}返回父数据;},getChildren(parentId,data){让孩子=[];for(letchildofdata){if(child.parentId===parentId){child.label=child.title;child.value=child.id;children.push(孩子);}}for(letchildofchildren){letchildrenCopy=this.getChildren(child.id,data);如果(childrenCopy.length>0){child.children=childrenC复制;}}返回孩子;另外,如果需要禁用某个节点后面的数据,可以传入其ID值进行过滤。代码如下:getParentData(data,id){letcloneData=JSON.parse(JSON.stringify(data));让parentData=[];for(letparentofcloneData){if(parent.parentId===null||parent.parentId===""){parent.label=parent.title;parent.value=parent.id;if(parent.id===id){parent.disabled=true;}else{parent.disabled=false;}让childrenCopy=this.getChildren(parent.id,cloneData,id,parent.disabled);如果(childrenCopy.length>0){parent.children=childrenCopy;}parentData.push(parent);}}返回父数据;},getChildren(parentId,data,id,isDisable){让孩子=[];for(letchildofdata){if(child.parentId===parentId){child.label=child.title;child.value=child.id;如果(isDisable){child.disabled=true;}else{if(child.id===id){child.disabled=true;}else{child.disabled=false;}}children.push(孩子);}}for(letchildofchildren){letchildrenCopy=this.getChildren(child.id,data,id,child.isDisable);如果(childrenCopy.length>0){child.children=childrenCopy;}}返回孩子;}针对某个id,在树的路径上寻找该ID,可用以下方法遍历寻找:getTreePath(tree,id){letpath=[];functiondfs(tree){for(leti=0;i
