Angular material 如何为角材质树视图中的每个节点设置id,以便每个节点都是独立的

Angular material 如何为角材质树视图中的每个节点设置id,以便每个节点都是独立的,angular-material,treeview,Angular Material,Treeview,我可以为角度材质树视图中的每个节点设置节点id,以便每个节点都是独立的 HTML代码如下所示: <mat-tree [dataSource]="dataSource" [treeControl]="treeControl"> <mat-tree-node *matTreeNodeDef="let node" matTreeNodePadding> <button mat-icon-button disabled></button>

我可以为角度材质树视图中的每个节点设置节点id,以便每个节点都是独立的

HTML代码如下所示:

<mat-tree [dataSource]="dataSource" [treeControl]="treeControl">
  <mat-tree-node *matTreeNodeDef="let node" matTreeNodePadding>
    <button mat-icon-button disabled></button>
     <mat-form-field appearance="legacy">
            <input matInput type="text" [formControl]="locationField"/>
            <mat-autocomplete #auto="matAutocomplete" >
              <mat-option *ngFor="let filteredFieldResult of locationFieldResults" [value]="filteredFieldResult">
                {{filteredFieldResult}}
              </mat-option>
            </mat-autocomplete>
    </mat-form-field>
  </mat-tree-node>
  <mat-tree-node *matTreeNodeDef="let node; when: hasChild" matTreeNodePadding>
    <button mat-icon-button
            [attr.aria-label]="'toggle ' + node.filename" matTreeNodeToggle>
      <mat-icon class="mat-icon-rtl-mirror">
        {{treeControl.isExpanded(node) ? 'expand_more' : 'chevron_right'}}
      </mat-icon>
    </button>
    {{node.item}}

  </mat-tree-node>
</mat-tree>
<button (click)="addNode()">Add Node</button>
<button (click)="addSubNode()">Add SubNode</button>
<button (click)="changeNode()">Change Node</button>

{{FilteredFeldResult}
{{treeControl.isExpanded(节点)?'expand_more':'chevron_right'}
{{node.item}
添加节点
添加子节点
变更节点

您可以将该id包含到正在创建的节点的结构中

例如:

export class MyFlatNode{
   constructor(public name: string, public level = 1, public expandable = false, public loadMoreParentItem: string | null = null, public id:number = 0, public parentId: number=0) {}
}
在为树创建节点时,我在material angular提供的transformer方法中使用了以下代码

newNode = new MyFlatNode(node.name, node.level, node.hasChildren, null, node.id, node.parentId);
如上所述,我已经传递了所有信息,如节点名称、节点级别和节点id等。您可以在树节点中传递可能需要的任何信息


您还可以使用适当的类构造函数对嵌套树节点执行相同的操作。

您可以将该id包含到正在创建的节点的结构中

例如:

export class MyFlatNode{
   constructor(public name: string, public level = 1, public expandable = false, public loadMoreParentItem: string | null = null, public id:number = 0, public parentId: number=0) {}
}
在为树创建节点时,我在material angular提供的transformer方法中使用了以下代码

newNode = new MyFlatNode(node.name, node.level, node.hasChildren, null, node.id, node.parentId);
如上所述,我已经传递了所有信息,如节点名称、节点级别和节点id等。您可以在树节点中传递可能需要的任何信息


您也可以通过使用适当的类构造函数对嵌套树节点执行相同的操作。

您可以进一步扩展独立的含义吗?我想他的意思是他希望通过ID区分节点,所以他能更容易地操纵它们。你能更详细地阐述一下你所说的独立吗?我想他的意思是他想用ID来区分节点,这样他就能更容易地操纵它们。