The tricky part here is to handle is the page event.
We can follow angular material documentation up to defining page event function.
Visit https://material.angular.io/components/paginator/examples
for the reference.
I will add my work with hours of hard work in this regard.
in the relevant HTML file should look like as follows,
<pre>
<mat-paginator
[pageSizeOptions]="pageSizeOptions"
[pageSize]="Size"
(page)="pageEvent = pageNavigations($event)"
[length]="recordCount">
</mat-paginator>
</pre>
Then go to the relevant typescript file and following code,
declarations,
@Input('data') customers: Observable<Customer[]>;
pageEvent: PageEvent;
Page: number=0;
Size: number=2;
recordCount: number;
pageSizeOptions: number[] = [2,3,4,5];
The key part of page navigation as follows,
pageNavigations(event? : PageEvent){
console.log(event);
this.Page = event.pageIndex;
this.Size = event.pageSize;
this.reloadData();
}
We require the following function to call data from the backend.
reloadData() {
this.customers = this.customerService.setPageSize(this.Page ,this.Size);
this.customerService.getSize().subscribe(res => {
this.recordCount = Number(res);
console.log(this.recordCount);
});
}
Before the aforementioned implementation, our services file should contain the following service call
setPageSize(page: number, size: number): Observable<any> {
return this.http.get(`${this.baseUrl}?pageSize=${size}&pageNo=${page}`);
}
Then all set to go and enable pagination in our app. Feel free to ask related questions thank you.