Pagination And Searching In Angular

In AngularJS, the $filter service is used to search the data in a table. In this blog, we will implement $filter for searching and maintaining the pagination. After implementing the pagination logic, it is very easy to implement the $filter service. But we need to keep in mind that the page number should be displayed based on the search. 

For example, let's say there are 1000 records and after searching, you got 100 records. On each page, you want to display 10 records, hence the number of pages should be displayed 10, not 100.

To get the accurate page number, use $filter first, before implementing your pagination logic with the list of the collection that you want to display in the table. Otherwise, you will get an invalid search data like in the below image. The below code example demonstrates how to implement pagination with search option.
 
  
  1. <HTML ng-app = "myapp">    
  2.        <TITLE> AngularJS Learning(Pagination with searching)</TITLE>    
  3.        <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.min.js"></script>    
  4.        <script>    
  5.              var myapp=angular.module("myapp",[]);    
  6.              myapp.filter('displayPageData', function() {    
  7.                 return function(input, start) {    
  8.                 start = +start; //parse to int    
  9.                 return input.slice(start);    
  10.                 }    
  11.              });    
  12.              myapp.controller("myappcont",function($scope,$filter){    
  13.                      $scope.currentPage = 0;    
  14.                      $scope.searchText='';    
  15.                      $scope.pageSize=10;    
  16.                      $scope.students=[];    
  17.                      for (var i=1; i<=100; i++) {    
  18.                                     if( i % 2 == 0){    
  19.                                          $scope.students.push({Name:"Student"+i,Gender:"Male",Class:i+"Std",Section:"A"});    
  20.                                     }    
  21.                                     else{    
  22.                                          $scope.students.push({Name:"Student"+i,Gender:"Female",Class:i+"Std",Section:"B"});    
  23.                                     }    
  24.                           }    
  25.              $scope.numberOfPages=function(){    
  26.                 var pageno=($filter('filter')($scope.students, $scope.searchText)).length/$scope.pageSize;    
  27.                 if(pageno==0){    
  28.                 pageno++;    
  29.                 }    
  30.                      return pageno;    
  31.                 }    
  32.            $scope.numberOfItems=function(){    
  33.      return ($filter('filter')($scope.students, $scope.searchText)).length; }    
  34.              });    
  35.        </script>    
  36.        <BODY ng-controller="myappcont">    
  37.            <h2>Student List</h2>    
  38.            <hr/>    
  39.            <table border="1" style="width:60%">    
  40.                 <caption>    
  41.                      Search : <input type="text" placeholder="Enter to search record" ng-model="searchText"/>    
  42.                 </caption>    
  43.                 <thead>    
  44.                      <tr>    
  45.                           <th>Name</th>    
  46.                           <th>Gender</th>    
  47.                           <th>Class</th>    
  48.                           <th>Section</th>    
  49.                      </tr>    
  50.                 </thead>    
  51.                 <tbody>    
  52.                      <tr ng-repeat="student in students|filter:searchText|displayPageData:currentPage*pageSize|limitTo:pageSize">    
  53.                           <td>{{student.Name}}</td>    
  54.                           <td>{{student.Gender}}</td>    
  55.                           <td>{{student.Class}}</td>    
  56.                           <td>{{student.Section}}</td>    
  57.                      </tr>    
  58.                 </tbody>    
  59.         </table>    
  60.         <table width="60%">    
  61.                           <tr>    
  62.                                <th width="10%">    
  63.                                     <button alignment="left" ng-disabled="currentPage == 0" ng-click="currentPage=currentPage-1">Previous</button>    
  64.                                </th>    
  65.                                <th width="80%">{{currentPage+1}}of {{numberOfPages()| number:0}}</th>    
  66.                                <th width="10%">    
  67.                                     <button alignment="left" ng-disabled="currentPage >= numberOfItems()/pageSize-1" ng-click="currentPage=currentPage+1">Next</button>    
  68.                                </th>    
  69.                           </tr>    
  70.                      </table>    
  71.       </BODY>    
  72.   </HTML>    
 
Next Recommended Reading Searching Data In Angular