Angular Material Desing components provide modern UI, optimized performance and lots of powerful feature with easy implementation. In this post, we are going through an example of how to use the Angular Material Data Table and Material Paginator for server-side pagination. Let's get started.
Simple API with Pagination
In this example, we will be using node js to create a simple API with pagination. Here is a simple node application, which response list of users. The users.js file contains an array of data for users with id, name and age. the /users endpoint is already configured for pagination, but without any query, it will response all the data. Hello Write some things here
As you are looking for the server-side pagination on the material table, I believe you have all the basic understanding required to set up a table with datasource and paginator. So let not waste any time here. Here is a table with mat paginator app.component.ts:
With this Setup, we fetch all the users data when the component initialized and use mat-paginator to display with pagination.
Mat-Paginator in Table, Angular Material
Implementing Serverside Pagination with Paginator
Now Let's talk about the API, we had created at the beginning of the article. The /users endpoint accepts two query offset and limit. And in every response, it sends back the total no of data. So that we can understand till when to request got the next data.
Offset is the position in the dataset of a particular record. and the limit is the required data length. http://localhost:3000/users?offset=1&limit=5 Here offset = 1, so 5 users are already loaded. requesting for next users from index 6 to 10 as limit = 5;
How can we use the mat-paginator to fetch the next data as required? mat paginator docs link. Going through the documentation we know that it emits an event page When the page is changed. We can make use to that.
First, when the data is loaded we have only 5 users, so the next page is blocked. We can set the length to the total from the server before assigning the data to datasource, in this way the mat paginator behaves like it has all the data. Got it? This is the core of this post. In simple word users.length = response.total This will add a null value for every data that is not available.
And in every page event, we make another API call and add the data in the required index. The event provides all the necessary data index, size and previous index.
getNextData(currentSize, offset, limit) is the methods you are interested in. As we had already set the length to total, pushing additional data will add it after the index (living those null value in the middle) so we should set back the size to currentSize and push data and set its size to the total.
Mat-Paginator with server side pagination, Data Table Angular
You can find the source code in this GitHub repo. That's all. You implement server-side pagination with material paginator in the material data table.
Thanks for the example! One correction: your code in github is correct, but on the web page, you need to add (page)="pageChanged($event)" to the mat-paginator element in the HTML.
6 Comments
Great artical
ReplyDeleteThanks for the comment. :)
DeleteThank you mate.. you saved my lot of efforts.
ReplyDeleteCan i get server side sorting
ReplyDeleteThanks for the example! One correction: your code in github is correct, but on the web page, you need to add (page)="pageChanged($event)" to the mat-paginator element in the HTML.
ReplyDeleteWell !! great.. now i gotta do the same in react...
ReplyDelete