首页 > 解决方案 > 带有复选框的 Angular Material mat-tree 全选

问题描述

我正在使用带有复选框的 Tree,并且我想添加一个按钮来检查所有复选框,我尝试了不同的方法但效果不佳,我能做到的最好的事情是:

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

rootItems 是一个根节点数组。

我可以看到在我迭代时选择了节点checklistSelection.selected,但是在浏览器中没有选中复选框,任何人都可以指出问题,谢谢。

标签: angularcheckboxtreeangular-material

解决方案


尝试以下方法

  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])
    }
  }

堆栈闪电战

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


推荐阅读