Get parent hierarchy from a child node in angular 6 material tree

后端 未结 3 1181
天涯浪人
天涯浪人 2021-02-15 12:06

I am following a tutorial to implement cdk tree in angular 6. I created a tree structure, now I want to get the parent hierarchy from from the child by making it clickable, whil

3条回答
  •  猫巷女王i
    2021-02-15 12:40

    Parent hierarchy can be stored in Array of numbers where each number is index of reccurent nested node. In order to expand tree on specified node I tried use previous examples, but eventually I decided make it that way:

    1) ChangePath must be call every time we click on node:

    changePath(node) {
    if (node.level < this.nodePath.length) {
      this.nodePath.splice(node.level, this.nodePath.length - node.level);
    }
    this.nodePath.push(this.treeControl.dataNodes.indexOf(node));}
    

    2) Next, when tree collapses, we must call expand of every item in nodePath (when tree collapse in is caused by delete node we dont want expand it, so last element is removed from path):

    expandTreeOnSpecifiedNode(isDelete: boolean) {
      if (isDelete) {
        this.nodePath.pop();
      }
      this.nodePath.forEach(id => {
        console.log(id);
        this.treeControl.expand(this.treeControl.dataNodes[id]);
        });
      }
    

提交回复
热议问题