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

后端 未结 3 1182
天涯浪人
天涯浪人 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:37

    Thanks to Flauwekeul, a little bit simplified

    import { FlatTreeControl } from '@angular/cdk/tree';
    
    export class CustomTreeControl extends FlatTreeControl {
      /**
       * Iterate over each node in reverse order and expand each inferior level nodes until level 0.
       */
      expandParents(node: any) {
          const currentLevel = this.getLevel(node);
    
          if (currentLevel < 1) {
              return null;
          }
    
          const startIndex = this.dataNodes.indexOf(node) - 1;
    
          for (let i = startIndex; i >= 0; i--) {
              const currentNode = this.dataNodes[i];
    
              if (this.getLevel(currentNode) < currentLevel) {
                  this.expand(currentNode);
                  if (this.getLevel(currentNode) === 0) break;
              }
          }
      }
    }
    

提交回复
热议问题