Angular:Mat-tree - 如何在初始加载期间选择所有复选框

问题描述 投票:0回答:1

由于 ngxtreeview 在 Angular 16+ 中已过时,我们现在正在研究 mat-tree。

数据加载到 mat-tree 后,根据业务规则,我们需要为少数节点设置默认检查,它可以是父节点或给定父节点的几个子节点。

https://stackblitz.com/angular/gabkadkvybq?file=app%2Ftree-checklist-example.ts

我们正在研究上面的示例,但我们不太确定如何在此处设置默认检查值

如果我们看看需求,它是基本需求,所以我可能在这里遗漏了一些东西。

checkbox treeview angular16
1个回答
0
投票

TreeChecklistExample

constructor(private database: ChecklistDatabase) {
  // Initialize data source and other properties
  this.database.initialize(this);
  this.selectAllCheckboxes(); // Call selectAllCheckboxes method here
}

//...other code

selectAllCheckboxes() {
  this.dataSource.data.forEach(node => this.selectRecursive(node, true));
}

selectRecursive(node: TodoItemNode, isSelected: boolean) {
  const flatNode = this.nestedNodeMap.get(node);
  if (flatNode) {
    this.checklistSelection.isSelected(flatNode) ? 
      this.checklistSelection.deselect(flatNode) :
      this.checklistSelection.select(flatNode);
  }
  if (node.children) {
    node.children.forEach(child => this.selectRecursive(child, isSelected));
  }
}

© www.soinside.com 2019 - 2024. All rights reserved.