How to do paging in AngularJS?

后端 未结 21 2367
轮回少年
轮回少年 2020-11-22 07:17

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

21条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-22 07:46

    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

    
    
    
    
    
    
    

    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

    
    
    

    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.

提交回复
热议问题