Angular Material 6.0.1 Tree default opened and expand/collapse all

后端 未结 2 1244
太阳男子
太阳男子 2021-02-08 05:39

I\'m using the Angular Material Tree in my project. Is it possible to have the tree opened by default.

And could there be a way to expand/collapse all the nodes at once

2条回答
  •  轮回少年
    2021-02-08 06:00

    MatTree's treeControl provide a expandAll method which you can use to expand all tree nodes, and collapseAll to close all tree nodes.

    You can can instance of MatTree via ViewChild and call expandAll in ngAfterViewInit life hook to make it expand by default.

    @ViewChild('tree') tree;
    
    ngAfterViewInit() {
      this.tree.treeControl.expandAll();
    }
    

    Source example for calling from template:

    
    
    
      ...
    
    

    see example.

提交回复
热议问题