Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do paging in AngularJS?

I have a dataset of about 1000 items in-memory and am attempting to create a pager for this dataset, but I'm not sure on how to do this.

I'm using a custom filter function to filter the results, and that works fine, but somehow I need to get the number of pages out.

Any clues?

like image 311
Micael Avatar asked May 30 '12 12:05

Micael


People also ask

How to add paging in AngularJS?

To display the pagination bar we have used ui. bootstrap as a dependency to the AngularJS application. The “curPage” is initialized with a value 1, it means whenever the webpage loads it will display the first page as a current-page.

How does pagination work in angular?

The pagination component is bound to items property of the app component using the Angular model binding attribute [items]="items" , and is bound to the onChangePage() method of the app component using the Angular event binding attribute (changePage)="onChangePage($event)" .


2 Answers

Angular UI Bootstrap - Pagination Directive

Check out UI Bootstrap's pagination directive. I ended up using it rather than what is posted here as it has enough features for my current use and has a thorough test spec to accompany it.

View

<!-- table here -->  <pagination    ng-model="currentPage"   total-items="todos.length"   max-size="maxSize"     boundary-links="true"> </pagination>  <!-- items/page select here if you like --> 

Controller

todos.controller("TodoController", function($scope) {    $scope.filteredTodos = []   ,$scope.currentPage = 1   ,$scope.numPerPage = 10   ,$scope.maxSize = 5;    $scope.makeTodos = function() {     $scope.todos = [];     for (i=1;i<=1000;i++) {       $scope.todos.push({ text:"todo "+i, done:false});     }   };   $scope.makeTodos();     $scope.$watch("currentPage + numPerPage", function() {     var begin = (($scope.currentPage - 1) * $scope.numPerPage)     , end = begin + $scope.numPerPage;      $scope.filteredTodos = $scope.todos.slice(begin, end);   }); }); 

I have made a working plunker for reference.


Legacy Version:

View

<!-- table here -->  <div data-pagination="" data-num-pages="numPages()"    data-current-page="currentPage" data-max-size="maxSize"     data-boundary-links="true"></div>  <!-- items/page select here if you like --> 

Controller

todos.controller("TodoController", function($scope) {    $scope.filteredTodos = []   ,$scope.currentPage = 1   ,$scope.numPerPage = 10   ,$scope.maxSize = 5;    $scope.makeTodos = function() {     $scope.todos = [];     for (i=1;i<=1000;i++) {       $scope.todos.push({ text:"todo "+i, done:false});     }   };   $scope.makeTodos();     $scope.numPages = function () {     return Math.ceil($scope.todos.length / $scope.numPerPage);   };    $scope.$watch("currentPage + numPerPage", function() {     var begin = (($scope.currentPage - 1) * $scope.numPerPage)     , end = begin + $scope.numPerPage;      $scope.filteredTodos = $scope.todos.slice(begin, end);   }); }); 

I have made a working plunker for reference.

like image 119
Scotty.NET Avatar answered Sep 19 '22 09:09

Scotty.NET


I recently implemented paging for the Built with Angular site. You chan checkout the source: https://github.com/angular/builtwith.angularjs.org

I'd avoid using a filter to separate the pages. You should break up the items into pages within the controller.

like image 36
btford Avatar answered Sep 20 '22 09:09

btford