How do I programmatically select Material Tree nodes with SelectionModel?

前端 未结 1 1689
自闭症患者
自闭症患者 2021-02-04 14:39

I have a tree with checkboxes (let\'s use Material\'s example here). I\'d like the fruit nodes to begin checked. How do I check those nodes?

I see that SelectionMo

1条回答
  •  北海茫月
    2021-02-04 15:12

    To pre-select the fruits node implement the following in an ngAfterViewInit for the TreeChecklistExample class in the attached stackblitz example.

    • This will loop through the dataNodes in the treeControl
    • If item == 'Fruits' select node and expand
    • Also if item == 'Groceries' expand node as it is the parent of Fruits.

      ngAfterViewInit() {
          for (let i = 0; i < this.treeControl.dataNodes.length; i++) {
            if (this.treeControl.dataNodes[i].item == 'Fruits') {
              this.todoItemSelectionToggle(this.treeControl.dataNodes[i]);
              this.treeControl.expand(this.treeControl.dataNodes[i])
            }
            if (this.treeControl.dataNodes[i].item == 'Groceries') {
              this.treeControl.expand(this.treeControl.dataNodes[i])
            }
          }
        }
      

    Stackblitz

    https://stackblitz.com/edit/angular-j2nf2r?embed=1&file=app/tree-checklist-example.html


    0 讨论(0)
提交回复
热议问题