I\'m new to angular and trying to implement pagination in my app. I am trying to use this material component.
With the code below, I can get length,
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,
Then go to the relevant typescript file and following code,
declarations,
@Input('data') customers: Observable;
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 {
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.