Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set Index column in Angular Mat-table (dataIndex shows 'NaN')

Mat-Table Working Perfect. Is there a way to add an auto increment Index. My Html for the code:

 <div class="mat-elevation-z8">


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

  <ng-container matColumnDef="no">
    <th mat-header-cell *matHeaderCellDef> No </th>
    <td mat-cell *matCellDef="let element; let i = index;"> {{i+1}} </td>
  </ng-container>
  <ng-container matColumnDef="Title">
    <th mat-header-cell *matHeaderCellDef mat-sort-header> Title </th>
    <td mat-cell *matCellDef="let element"> <a (click)="openDescriptionView(element.title, element.imageUrl, element.description)">{{element.title}}</a>
    </td>
  </ng-container>

  <ng-container matColumnDef="destination">
    <th mat-header-cell *matHeaderCellDef mat-sort-header> Destination </th>
    <td mat-cell *matCellDef="let element"> {{element.destination}} </td>
  </ng-container>

  <ng-container matColumnDef="startDate">
    <th mat-header-cell *matHeaderCellDef mat-sort-header> Start Date </th>

    <td mat-cell *matCellDef="let element"> {{element.startDate}}</td>
  </ng-container>

  <ng-container matColumnDef="endDate">
    <th mat-header-cell *matHeaderCellDef mat-sort-header> End Date </th>

    <td mat-cell *matCellDef="let element">{{element.endDate}}</td>
  </ng-container>

  <ng-container matColumnDef="price">
    <th mat-header-cell *matHeaderCellDef mat-sort-header> price </th>
    <td mat-cell *matCellDef="let element">{{element.price}}</td>
  </ng-container>


  <ng-container matColumnDef="setting">
    <th mat-header-cell *matHeaderCellDef> Settings </th>
    <td mat-cell *matCellDef="let element">
      <a class="myIcon" (click)="editOffer(element.id)"><i class="pe-7s-pen"></i></a>
      <a class="myIcon" (click)="deleteOffer(element.id, element.title)"><i class="pe-7s-trash"></i></a>

    </td>
  </ng-container>

  <tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
  <tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
</table>
<mat-paginator [pageSizeOptions]="[10, 20]" showFirstLastButtons></mat-paginator>

I go to the next page it starts again from one. If I am showing 10 item per page then after goes to next page it starts with one instead of eleven. I also tried let i=dataIndex and that shows 'NaN'. Please help.

like image 241
Syamkumar S Avatar asked Dec 20 '18 06:12

Syamkumar S


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. Selector: [matColumnDef]

How can I get data from mat-table?

Begin by adding the <table mat-table> component to your template and passing in data. The simplest way to provide data to the table is by passing a data array to the table's dataSource input. The table will take the array and render a row for each object in the data array.

Which can be used in mat-table for sorting?

With the help of MatSortModule we can add sorting to mat-table in Angular. MatSortModule is separate component in Angular material and we have to import it from @angular/material/sort . import {MatSortModule} from '@angular/material/sort'; As it's an individual component, we can use it to sort simple tables as well.


2 Answers

You could use a cell that calculates the running index like this:

<td mat-cell *matCellDef="let element; let i = index;"> 
  {{paginator.pageIndex * paginator.pageSize + i + 1}} 
</td>

With a paginator like this:

<mat-paginator #paginator [pageSizeOptions]="[10, 20]" showFirstLastButtons></mat-paginator>

Here is a stackblitz that demonstrates this.

like image 175
Fabian Küng Avatar answered Sep 23 '22 19:09

Fabian Küng


You can set a pageIndex variable initialized to one for first page... on each click of 'next page' set it to pageIndex = pageIndex + 10; foir each click of 'back page' set it to pageIndex = pageIndex - 10;

like image 33
Akber Iqbal Avatar answered Sep 23 '22 19:09

Akber Iqbal