简体   繁体   English

Angular 材质树中的预选框

[英]Precheck box in Angular Material Tree

I have an Angular Material Tree I'm displaying.我有一个正在显示的 Angular Material Tree。 The tree works fine by itself, but I also have a list of names/ids that are already selected values.该树本身可以正常工作,但我也有一个名称/ID 列表,这些名称/ID 已经是选定的值。 The data is given to me separately.数据是单独给我的。 I haven't been able to figure out how to precheck boxes inside the tree on load.我一直无法弄清楚如何在加载时预先选中树内的框。

I've created a Stackblitz here to show my issue: https://stackblitz.com/edit/angular-ukaq9u我在这里创建了一个 Stackblitz 来展示我的问题: https ://stackblitz.com/edit/angular-ukaq9u

My initial thought was to try and look through my selected list and manually toggle the selected in ngInit like so (this doesn't work though):我最初的想法是尝试查看我选择的列表并像这样手动切换 ngInit 中的选择(虽然这不起作用):

    for (const pg of selectedBoxes) {
      let leaf = new TodoItemFlatNode();
      leaf.item = pg.Product_Group_ID.toString();
      leaf.level = 1;
      leaf.expandable = false;
      this.todoLeafItemSelectionToggle(leaf);
    }

Does anyone have any other suggestions?有没有人有其他建议? I haven't been able to find any examples online.我一直无法在网上找到任何示例。

Finally found a similar example of what I was looking for here.终于找到了我在这里寻找的类似示例

By looping through my existing nodes and comparing them to my existing list of selected data;通过循环我现有的节点并将它们与我现有的选定数据列表进行比较; I can use the existing node, instead of creating a new copy, to toggle the check.我可以使用现有节点而不是创建新副本来切换检查。

Like so:像这样:

    for (let i = 0; i < this.treeControl.dataNodes.length; i++) {
      for (const pg of selectedBoxes) {
        if (this.treeControl.dataNodes[i].item == pg.Product_Group_Name) {
          this.todoItemSelectionToggle(this.treeControl.dataNodes[i]);
          this.treeControl.expand(this.treeControl.dataNodes[i])
        }
      }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM