Angular js providing a very easy way to do client side pagination.Here i am going to explain about simple client side pagination using angular js. I have explained the same using Angular 2(+) concept as well, if interested please check this link.
Example of Controller
In controller we are assigning the json data to datalist
Note: $scope.curPage and $scope.pageSize
angular.module('sampleproject').controller( 'SampleController',
function ($rootScope, $scope ,samplefactoryService )
{
// pagination
$scope.curPage = 0;
$scope.pageSize = 5;
$scope.onSample = function()
{
samplefactoryService.myServicefunction( $scope.list ,
function(data)
{
$scope.datalists = data ;// response from service
},
function( data)
{
//error
});
};
$scope.numberOfPages = function()
{
return Math.ceil($scope.datalists.length / $scope.pageSize);
};
});
HTML ( View )
<ul>
<li ng-repeat="datalist in datalists |
pagination: curPage * pageSize | limitTo: pageSize" >
<div>{{ datalist.name }}</div> // your content
</li>
</ul>
Pagination div
<--Pagination div --->
<div class="pagination-div" ng-show="datalists.length">
<ul>
<li>
<button type="button" ng-disabled="curPage == 0"
ng-click="curPage=curPage-1">PREV</button>
</li>
<li>
<span>Page {{curPage + 1}} of {{ numberOfPages() }}</span>
</li>
<li>
<button type="button"
ng-disabled="curPage >= datalists.length/pageSize - 1"
ng-click="curPage = curPage+1">NEXT </button>
</li>
</ul>
</div>
Directive
You can declare this in filter.js or controller itself .Better define this in directive .
angular.module('sampleproject').filter('pagination', function()
{
return function(input, start)
{
start = +start;
return input.slice(start);
};
});
You can modify the style for better look and feel
1. Angular js client side pagination like Google
2. Angular js simple show more pagination
Related Posts
1. Communicate with controllers in angular js
2. Rating Stars using angular js Directive concept
5. Filtering concept in angular js
6. Save highchart as binary image
fiddle tutorial is very needful..........thanx..
ReplyDeletethanx........
ReplyDeleteI cant understand this part can u explain ??
ReplyDeleteangular.module('sampleproject').filter('pagination', function()
{
return function(input, start)
{
start = +start;
return input.slice(start);
};
});
This is angular js custom filter , you can give any name ( i given pagination) with ur module name .You can create a separate filter.js file to keep these customized angular filters .
DeleteFor example this is another filter
http://angulartutorial.blogspot.in/2014/04/date-filtering-and-formatting-in.html
Simple and works great, thanks! However, I'm getting an error that "input is undefined" and adding
ReplyDeleteif (!input || !input.length) { return; }
fixed the issue.
angular.module('sampleproject').filter('pagination', function()
{
return function(input, start)
{
if (!input || !input.length) { return; }
start = +start;
return input.slice(start);
};
});
Thanks for the fix .
DeleteThanks so much.. this is very helpful. Have one error though with the following code part------$scope.numberOfPages = function()
ReplyDelete{
return Math.ceil($scope.datalists.length / $scope.pageSize);
};
});
---
this generates following :Error: Error: [$interpolate:interr] Can't interpolate: Page {{curPage +1 }} of {{numberOfPages()}}
it does this in a loop that does not seem to stop.
Anyone else has this?
Yes, Can you please help
DeleteThank you very much, really useful, I'd recommend to add @Harry Bundles's comment as part of the guide as it a common error.
ReplyDeleteGreat buddy
ReplyDeleteThank you for this very useful post. I used it in a recent project (I am learning AngularJS) and it was so helpful I mentioned it in my blog posting http://bit.ly/1Gzjcbu
ReplyDeleteThank you very much
DeleteGreat prashop...
ReplyDeleteCan anyone please help me in my below query
ReplyDeleteI have DataTable grid on my page and deleting the row from it. After deleting the row I want to stay on same page index. To achieve this I use
table.row('.selected').remove().draw( false );
but it is not working in Angular.
This comment has been removed by the author.
ReplyDeleteIts really useful for me. thanks. http://www.phptutorials.club
ReplyDeleteIt's really great, but what if I want to print all pages
ReplyDeletemy code is as follow but it prints only first page
$scope.printData= function()
{
console.log("print");
var divToPrint=document.getElementById("reportTablePrint");
newWin= window.open("");
newWin.document.write(divToPrint.outerHTML);
newWin.print();
newWin.close();
};
Great post...
ReplyDeletethank you.. i have implemented and working fine.
ReplyDeleteThank you , its a great tutorial.
ReplyDeletehow to write multiselect checkbox in angularjs.any one help me.
ReplyDeleteAre you looking something similar this
Deletehttp://www.angulartutorial.net/2015/10/angular-select-allde-select-all-checkbox.html
Hi Prashobh,
ReplyDeleteIt's working perfectly with the hard coded values in the controller. But it is failing when we get the data from server using $http ajax. can you please add some sample with ajax. Thanks in advance..
sorry for the previous comment.. it is working perfectly.. thank you..
ReplyDeleteThanks a lot. The code is working fine:)
ReplyDeletethe css for pagination centered is missing.
ReplyDeleteCan you please provide me the full css code
This tutorial is awesome..its working perfectly fine,Thanks a lot :)
ReplyDeleteThanks....Its very helpfull..
ReplyDeleteng-disable is not working,it's going to previous wlile in page 0 and data also showing in page 0 and also same for next button.
ReplyDeletegood thanks
ReplyDeleteI get same set of data on all the Pages.It is Because
ReplyDeleteapp.filter('startFrom', function () {
return function (input, start) {
if (!input || !input.length) { return; }
start = +start; //parse to int
return input.slice(start);
}
});
here start always gets 0 it is not getting incemented? Any Idea?