Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular mat tree on matTreeNodeDef iteration not able to get index as of ngfor

I have an angular material tree. On which i have a code like below

<mat-tree-node *matTreeNodeDef="let node; let idx=index">
  {{node.filename}}::{{idx}}
</mat-tree-node>

Here idx not populating

like image 633
androidGenX Avatar asked Apr 03 '19 08:04

androidGenX


2 Answers

I'm afraid *matTreeNodeDef is not same as *ngFor directive.

*matTreeNodeDef directive accepts 2 Inputs.

First being TreeNode<T> which can be defined as

<mat-tree-node *matTreeNodeDef="let node">
  ...
</mat-tree-node>

where node represents the TreeNode object.

Second being matTreeNodeDefWhen which is a function to filter the Tree nodes and can be defined as

HTML

<mat-tree-node *matTreeNodeDef="let node; when hasChild">
  ...
</mat-tree-node>

TS

hasChild = (_: number, node: FlatNode) => node.expandable;  // returns a boolean value

Generally speaking, a tree structure does not have an index.

Reference: https://material.angular.io/components/tree/api#MatTreeNodeDef

like image 111
T. Sunil Rao Avatar answered Sep 25 '22 13:09

T. Sunil Rao


you will need to add index to the node with a transform function like: https://stackoverflow.com/a/57142368/11692089

like image 43
mr.vea Avatar answered Sep 25 '22 13:09

mr.vea