how to define index in angular material table

pushplata patel picture pushplata patel · Apr 4, 2018 · Viewed 62.4k times · Source

how should I define an index variable when angular material table is used as ngFor is not used in this table.

I did search for it in the documentation but index is not mentioned any where in it.

where and how do i define an index variable for the array that I used in this table, so that I can bind the index value in my table itself.

Answer

ritaj picture ritaj · Apr 4, 2018

Can you add index to let element; let i = index;" as you'd do with *ngFor?

<mat-row *matRowDef="let row; columns: displayedColumns; let i = index"></mat-row>

Or like so:

<ng-container matColumnDef="index">
  <mat-header-cell *matHeaderCellDef> Index </mat-header-cell>
  <mat-cell *matCellDef="let element; let i = index;">{{i}}</mat-cell>
</ng-container>

Working Example: https://stackblitz.com/edit/angular-acdxje?file=app/table-basic-example.html