How to do paging in AngularJS?

后端 未结 21 2178
轮回少年
轮回少年 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:54

    There is my example. Selected button in the middle on the list Controller. config >>>

     $scope.pagination = {total: null, pages: [], config: {count: 10, page: 1, size: 7}};
    

    Logic for pagination:

    /*
         Pagination
         */
        $scope.$watch('pagination.total', function (total) {
            if(!total || total <= $scope.pagination.config.count) return;
            _setPaginationPages(total);
        });
    
        function _setPaginationPages(total) {
            var totalPages = Math.ceil(total / $scope.pagination.config.count);
            var pages = [];
            var start = $scope.pagination.config.page - Math.floor($scope.pagination.config.size/2);
            var finish = null;
    
            if((start + $scope.pagination.config.size - 1) > totalPages){
                start = totalPages - $scope.pagination.config.size;
            }
            if(start <= 0) {
                start = 1;
            }
    
           finish = start +  $scope.pagination.config.size - 1;
           if(finish > totalPages){
               finish = totalPages;
           }
    
    
            for (var i = start; i <= finish; i++) {
                pages.push(i);
            }
    
            $scope.pagination.pages = pages;
        }
    
        $scope.$watch("pagination.config.page", function(page){
            _setPaginationPages($scope.pagination.total);
            _getRespondents($scope.pagination.config);
        });
    

    and my view on bootstap

    
    

    It is useful

提交回复
热议问题