25

I am trying to go to the first page of the mat-paginator, that is, reset the pagination, but it does not work. Any idea how it can be solved?

My html is such that

<mat-paginator [length]="itemTotal" [pageIndex]="page" [pageSize]="itemPage" (page)="pageEvent = getPublicationFollowersData($event)">
    </mat-paginator>

The typescript:

   getPublicationFollowersData(event?: PageEvent) {
      this.getPublicationsUser(event.pageIndex);
}

and i try to initialize the page with:

this.page = 1

But it does not work.

Edric
  • 24,639
  • 13
  • 81
  • 91
MGs
  • 453
  • 1
  • 5
  • 7

5 Answers5

39

You need to use a ViewChild decorator to select the paginator and then set the pageIndex property of the paginator.

@ViewChild() paginator: MatPaginator;
...
this.paginator.pageIndex = 0;

Edit: As suggested by the other answers, if you would like to reset the paginator to the first page, it would be better to use this.paginator.firstPage()

Stromwerk
  • 710
  • 8
  • 18
14
<mat-paginator #paginator [length]="length"
               [pageSize]="pageSize"
               [pageSizeOptions]="pageSizeOptions"
               (page)="pageEvent = paginationClicked($event)">
</mat-paginator>
@ViewChild('paginator') paginator: MatPaginator;
this.paginator.firstPage();
Shubham Verma
  • 141
  • 1
  • 2
10

Create a referecne for material angular paginator along with data source

dataSource: MatTableDataSource<any>;
@ViewChild(MatPaginator) paginator: MatPaginator;

You want to reset the paginator using the following code

this.dataSource.paginator = this.paginator;

You can also reset by calling the first page method of the paginator

this.paginator.firstPage();
Rohan Shenoy
  • 805
  • 10
  • 23
1

if using

@ViewChild(MatPaginator, { static : true} ) paginator: MatPaginator;

it should be

@ViewChild(MatPaginator, { static : false} ) paginator: MatPaginator;

if static:true the paginator object is undefined.

0
@ViewChild('paginator') paginator: MatPaginator;

 this.paginator.firstPage();

<mat-paginator #paginator [length]="length"
           [pageSize]="pageSize"
           [pageSizeOptions]="pageSizeOptions"
           (page)="pageEvent = paginationClicked($event)">

now you can reset the pagination using below

    setTimeout(()=>{this.paginator.pageIndex = 0;});
Kumara
  • 471
  • 2
  • 10
  • 33