0

I am trying to apply pagination on the data table but its not working as expected.

I have been trying to apply pagination through @viewChild. have imported necessary module.

This is my code...


import {MatPaginator} from '@angular/material';
import {MatPaginatorModule} from '@angular/material/paginator';
import {MatTableDataSource} from '@angular/material';

OFFER_DATA = new MatTableDataSource<IOfferItems>(this.OFFER_DATA);

@ViewChild(MatPaginator) paginator: MatPaginator;

constructor(private _offerService: OfferService){}

ngOnInit() {
    this._offerService.getOffers()
      .subscribe((offerData) => {
        this.OFFER_DATA = offerData;
        this.OFFER_DATA.paginator = this.paginator;
      });
  }



HTML - 

<table mat-table [dataSource]="OFFER_DATA" matSort class="mat-elevation-z8">
  <ng-container matColumnDef="id">
    <th mat-header-cell *matHeaderCellDef mat-sort-header> ID </th>
    <td mat-cell *matCellDef="let offer"> {{offer.id}} </td>
  </ng-container>

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

  <tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
  <tr mat-row *matRowDef="let row; columns: displayedColumns;"
      (click)="selection.toggle(row)">
  </tr>
</table>
<mat-paginator [pageSizeOptions]="[5, 10, 20]" showFirstLastButtons>
</mat-paginator>
4
  • provide HTML content also Commented Dec 31, 2018 at 9:04
  • Done Please check!! Commented Dec 31, 2018 at 9:27
  • it's working for me according to your code stackblitz.com/edit/… Commented Dec 31, 2018 at 10:22
  • It works fine with hard-coded data. In my case the data is dynamic. Commented Dec 31, 2018 at 10:35

1 Answer 1

1

use #paginator as

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



@ViewChild(MatPaginator) paginator: MatPaginator;

here is a sample code of mine mat-table-sort-paginator-filter

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.