Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to define index in angular material table

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.

like image 943
pushplata patel Avatar asked Apr 04 '18 10:04

pushplata patel


People also ask

What is MatColumnDef in angular?

MatColumnDef extends CdkColumnDef Column definition for the mat-table. Defines a set of cells available for a table column.

What is multiTemplateDataRows?

multiTemplateDataRows have a different variable to hold the row's index called dataIndex . Access the row's index using let k = dataIndex . <!-- Row content--> <mat-row *matRowDef="let row; columns: displayedColumns; let k = dataIndex;" matRipple (click)="this.expandRow(row, k)"> </mat-row>

What is material table in angular?

The mat-table provides a Material Design styled data-table that can be used to display rows of data. This table builds on the foundation of the CDK data-table and uses a similar interface for its data input and template, except that its element and attribute selectors will be prefixed with mat- instead of cdk- .


3 Answers

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

like image 63
Roberto Zvjerković Avatar answered Oct 20 '22 21:10

Roberto Zvjerković


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

like image 118
adam0101 Avatar answered Oct 20 '22 22:10

adam0101


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".

like image 12
Daniel Danielecki Avatar answered Oct 20 '22 22:10

Daniel Danielecki