how to define index in angular material table
Asked Answered
B

12

99

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.

  <mat-table #table [dataSource]="dataSource">

    <!--- Note that these columns can be defined in any order.
          The actual rendered columns are set as a property on the row definition" -->

    <!-- Position Column -->
    <ng-container matColumnDef="position">
      <mat-header-cell *matHeaderCellDef> No. </mat-header-cell>
      <mat-cell *matCellDef="let element"> {{element.position}} </mat-cell>
    </ng-container>

    <!-- Name Column -->
    <ng-container matColumnDef="name">
      <mat-header-cell *matHeaderCellDef> Name </mat-header-cell>
      <mat-cell *matCellDef="let element"> {{element.name}} </mat-cell>
    </ng-container>

    <!-- Weight Column -->
    <ng-container matColumnDef="weight">
      <mat-header-cell *matHeaderCellDef> Weight </mat-header-cell>
      <mat-cell *matCellDef="let element"> {{element.weight}} </mat-cell>
    </ng-container>

    <!-- Symbol Column -->
    <ng-container matColumnDef="symbol">
      <mat-header-cell *matHeaderCellDef> Symbol </mat-header-cell>
      <mat-cell *matCellDef="let element"> {{element.symbol}} </mat-cell>
    </ng-container>

    <mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
    <mat-row *matRowDef="let row; columns: displayedColumns;"></mat-row>
  </mat-table>
</div>

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.

Barreto answered 4/4, 2018 at 10:5 Comment(1)
Can you handle the index value and set it when you fetch the data from the API?Mcmath
M
190

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

Minaminabe answered 4/4, 2018 at 10:22 Comment(5)
Works! But I'd love to know where this came from....how did you know there's a variable index available to be consumed? Can someone explain this further please?Genie
Take care that it will repeat per page if you enabled paginationFelten
if you have pagination you can calculate row index like {{this.paginator.pageIndex * this.paginator.pageSize + i}}Pix
let i=dataIndex this is working for me.Horsehair
As mentioned in a comment above, please remove "this" keyword and try as {{paginator.pageIndex * paginator.pageSize + i}} and will work.!Godly
W
135

For anyone who has set the multiTemplateDataRows property of mat-table to true, you can't use index. Instead you have use either dataIndex or renderIndex.

<mat-cell *matCellDef="let row; let i = dataIndex;">{{i}}</mat-cell>

See https://github.com/angular/material2/issues/12793

Woosley answered 28/11, 2018 at 18:1 Comment(1)
Take care that it will repeat per page if you enabled paginationFelten
P
13

For those who are facing problem with keeping the right index when using pagination which table has more than 1 page. It can be especially important when you have editable element, thus you're using a routerLink to add/edit/delete selected elements.

<ng-container matColumnDef="title">
 <mat-header-cell *matHeaderCellDef mat-sort-header>Title</mat-header-cell>
 <mat-cell *matCellDef="let book; let i = index;" fxLayoutAlign.lt-md="center center">
  <button mat-button [routerLink]="[i + (paginator.pageIndex * paginator.pageSize)]" routerLinkActive="active"</button>
 </mat-cell>
</ng-container>

As well as

<mat-paginator #paginator [pageSize]="5" [pageSizeOptions]="[5, 10, 25, 100]"></mat-paginator>

In essence, i + (paginator.pageIndex * paginator.pageSize) is the solution, but it counts from 0. If you'd like to index from 1, simply make it (i+1) + (paginator.pageIndex * paginator.pageSize). Worth to note is that you really need the #paginator and [pageSize]="VALUE".

Philodendron answered 29/6, 2019 at 21:39 Comment(2)
<td td mat-cell *matCellDef="let index = index">{{ (index + 1) + (paginator.pageIndex * paginator.pageSize) }}</td> works best for paginated data table. thanks.Toreutics
Worth to note is that you really need the #paginator and [pageSize]="VALUE". This is the real key.Keverne
E
8

From Angular 5 you can alias index to local variable i using index as i

<ng-container matColumnDef="rowIndex">
  <mat-header-cell *matHeaderCellDef> Index </mat-header-cell>
  <mat-cell *matCellDef="let element;index as i;"> {{ i }} </mat-cell>
</ng-container>
Enid answered 5/4, 2019 at 15:57 Comment(0)
D
8

If someone using <mat-paginator ../>, then index can be interpreted as below

<mat-cell *matCellDef="let element; index as i"> {{paginator.pageSize * paginator.pageIndex + i + 1}}</mat-cell>
Depilate answered 10/10, 2019 at 9:31 Comment(0)
G
3

If you are using pagination in your mat-table,

<ng-container matColumnDef="index">
   <mat-header-cell *matHeaderCellDef> Index </mat-header-cell>
   <mat-cell *matCellDef="let element; index as i">
        {{tableOnePaginator.pageSize * tableOnePaginator.pageIndex + i + 1}}</mat-cell>
</ng-container>

In here, this tableOnePaginator is need to define in your ts component before using it. And make sure you have set that to your dataSource as follows.

this.dataSource.paginator = this.tableOnePaginator;
Godly answered 9/6, 2021 at 4:22 Comment(0)
I
3

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>

Also Add index column.

<ng-container matColumnDef="index">
  <mat-header-cell *matHeaderCellDef> Index </mat-header-cell>
  <mat-cell *matCellDef="let element; let i = index;">{{i + 1}}</mat-cell>
</ng-container>
Indoxyl answered 18/4, 2023 at 11:12 Comment(0)
A
2

If you are using paginator and you want to place index with every page change then go with this below code; In .HTML file:-

<ng-container matColumnDef="Position">
   <th mat-header-cell *matHeaderCellDef> # </th>
   <td mat-cell *matCellDef="let element; let i= index"> {{i + 1 +
     (thirdPaginator.pageIndex*thirdPaginator.pageSize) }}</td>
</ng-container>

Army answered 4/4, 2018 at 10:5 Comment(0)
I
0

If you are use paginator try this:

<ng-container matColumnDef="position">
  <th mat-header-cell *matHeaderCellDef> Position </th>
  <td mat-cell *matCellDef="let element; let i = index"> {{(i+1) + (paginator.pageIndex*paginator.pageSize)}} </td>
</ng-container>

this working perfectly for Angular version 8.you must use #paginator and [pageSize]="VALUE"

Interdigitate answered 2/1, 2021 at 10:3 Comment(0)
D
0

<td mat-cell *matCellDef="let row; let indx = dataIndex">

When using the multiTemplateDataRows directive to support multiple rows for each data object, the context of *matRowDef is the same except that the index value is replaced by dataIndex and renderIndex.

Dahlgren answered 28/5, 2021 at 2:31 Comment(0)
E
0
<ng-container matColumnDef="Index">
   <th mat-header-cell *matHeaderCellDef> # </th>
   <td mat-cell *matCellDef="let element; index as i;">
      {{_paginator.pageSize * _paginator.pageIndex + i + 1}}    
   </td>
</ng-container>

Heading

Evincive answered 6/10, 2022 at 3:22 Comment(0)
B
0

In Angular 14, unique declaration that worked in my case is:

<td mat-cell *matCellDef="let row; let i = dataIndex">

In other parts of my app, index as i also works, but not always. I heard that using let i = index is written in old versions of Angular

Blow answered 21/7, 2023 at 12:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.