Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

reset paginator first page angular material

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.

like image 890
MGs Avatar asked Jan 27 '19 19:01

MGs


People also ask

How do I reset my Paginator?

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

How do I change the page size on a mat Paginator label?

The labels for the paginator can be customized by providing your own instance of MatPaginatorIntl . This will allow you to change the following: The label for the length of each page. The range text displayed to the user.


2 Answers

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()

like image 198
Stromwerk Avatar answered Oct 23 '22 17:10

Stromwerk


<mat-paginator #paginator [length]="length"
               [pageSize]="pageSize"
               [pageSizeOptions]="pageSizeOptions"
               (page)="pageEvent = paginationClicked($event)">
</mat-paginator>
@ViewChild('paginator') paginator: MatPaginator;
this.paginator.firstPage();
like image 22
Shubham Verma Avatar answered Oct 23 '22 17:10

Shubham Verma