Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular Material mat-tree with checkboxes select all

I'm using Tree with checkboxes, and I want to add a button to check all checkboxes, I tried different methods but no good, the best thing i was able to achieve is this:

  selectAllFiscal() {
    this.rootItems.forEach(node => {
      this.todoItemSelectionToggle(node);
    });
  }

rootItems is an array of root nodes.

I can see that nodes are selected when I iterate checklistSelection.selected, but the checkboxes are not selected in the browser, can anybody point the problem, thanks.

like image 593
Abdennacer Lachiheb Avatar asked Mar 04 '23 22:03

Abdennacer Lachiheb


1 Answers

Try the following for your method

  checkAll(){
    for (let i = 0; i < this.treeControl.dataNodes.length; i++) {
      if(!this.checklistSelection.isSelected(this.treeControl.dataNodes[i]))
        this.checklistSelection.toggle(this.treeControl.dataNodes[i]);
      this.treeControl.expand(this.treeControl.dataNodes[i])
    }
  }

Stackblitz

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

like image 106
Marshal Avatar answered Mar 16 '23 10:03

Marshal