Pagination with $limitTo filter in AngularJS


In my previous article, we demonstrated how to implement pagination with setting static limit of records. In this blog we will demonstrate how to set the limit of record dynamically with help of $limitTo filter.

Getting Started

We all are know that the AngularJS built in $limitTo filter is used to limit the number of record or string to display in an array or a collection. Fore this reason in my previous article I have used this $limitTo filter for implementing pagination in html table, but there we can see I have used a static number that is 5.

This article demonstrates how to set that limit to dynamic for limit of record to display in pagination. Go through the below code example for more details
 <HTML ng-app = "myapp">  
       <TITLE> AngularJS Learning(Pagination with $limitTo filter)</TITLE>  
       <script src=""></script>  
             var myapp=angular.module("myapp",[]);  
             myapp.filter('displayPageData', function() {  
                return function(input, start) {  
                start = +start; //parse to int  
                return input.slice(start);  
                     $scope.currentPage = 0;  
                     for (var i=1; i<=100; i++) {  
                                    if( i % 2 == 0){  
                var pageno=($filter('filter')($scope.students, $scope.searchText)).length/$scope.pageSize;  
                     return pageno;  
     return ($filter('filter')($scope.students, $scope.searchText)).length; }  
       <BODY ng-controller="myappcont">  
           <h2>Student List</h2>  
           <table border="1" style="width:60%">  
                     Search : <input type="text" placeholder="Enter to search record" ng-model="searchText"/>  
                     No of Rows : <input type="number" step=5 min=5 max=20 ng-model="pageSize"/>  
                     <tr ng-repeat="student in students|filter:searchText|displayPageData:currentPage*pageSize|limitTo:pageSize">  
        <table width="60%">  
                               <th width="10%">  
                                    <button alignment="left" ng-disabled="currentPage == 0" ng-click="currentPage=currentPage-1">Previous</button>  
                               <th width="80%">{{currentPage+1}}of {{numberOfPages()| number:0}}</th>  
                               <th width="10%">  
                                    <button alignment="left" ng-disabled="currentPage >= numberOfItems()/pageSize-1" ng-click="currentPage=currentPage+1">Next</button>  


Hope this article may helpful to you, happy coding and enjo.........y.

Kailash Chandra Behera

No comments:

Post a Comment