DirPaginate is the perfect solution for meeting the requirements outlined in this query.
To see a demonstration, please visit this Plunker link.
If you are implementing server-side pagination, ensure that your JSON response follows this specific format.
JSON Response Format:
{
Count: 1400,
Items: [
{ // item 1... },
{ // item 2... },
{ // item 3... },
...
{ // item 25... }
]
}
The key factor to note is that the total count of items from your database is crucial for passing it to our directive.
Angular Controller Format:
.controller('UsersController', function($scope, $http) {
$scope.users = [];
$scope.totalUsers = 0;
$scope.usersPerPage = 25; // should correspond to API results per page
getResultsPage(1);
$scope.pagination = {
current: 1
};
$scope.pageChanged = function(newPage) {
getResultsPage(newPage);
};
function getResultsPage(pageNumber) {
$http.get('path/to/api/users?page=' + pageNumber)
.then(function(result) {
$scope.users = result.data.Items;
$scope.totalUsers = result.data.Count
});
}
})
Below is an example of how to integrate it into your HTML structure.
HTML Integration:
<div ng-controller="UsersController">
<table>
<tr dir-paginate="user in users | itemsPerPage: usersPerPage" total-items="totalUsers" current-page="pagination.current">
<td>{{ user.name }}</td>
<td>{{ user.email }}</td>
</tr>
</table>
<dir-pagination-controls on-page-change="pageChanged(newPageNumber)"></dir-pagination-controls>
</div>
Refer to the "Working with Asynchronous Data" section on this page for further guidance.