您可以尝试遍历整个树以查找节点,例如,您可以编写一些函数,例如:
public unselectParents(searchNode: TreeNode, searchPivot: TreeNode): boolean {
if (searchPivot === searchNode) return true;
else if (searchPivot.children.length !== 0) {
for (let i = 0; i < searchPivot.children.length; i++) {
let value = this.unselectParents(searchNode, searchPivot.children[i]);
if (value === true) {
searchPivot.check = false;
return true;
}
}
}
return false;
}
然后在 selectNode 方法中调用这个方法:
public selectNode(node: TreeNode, value: boolean): void {
this.check(node, value);
if (value == false) {
this.unselectParents(node, this.data.root);
}
}
或者,您也可能在“TreeNode”界面中有一个属性“parent:TreeNode”。