如何使用AngularJS解决分页错误?

时间:2015-01-06 10:59:08

标签: javascript angularjs

我正在使用AngularJS,这是我的代码:

控制器

$scope.totalItems = $scope.lists.length;
$scope.currentPage = 1;
$scope.numPerPage = 8;

$scope.paginate = function (value) {
    var begin, end, index;
    begin = ($scope.currentPage - 1) * $scope.numPerPage;
    end = begin + $scope.numPerPage;
    index = $scope.lists.indexOf(value);
    return (begin <= index && index < end);
}

索引

<pagination total-items="totalItems" ng-model="currentPage"
            max-size="5" boundary-links="true"
            items-per-page="numPerPage" class="pagination-sm">
</pagination>

截图

enter image description here

我的问题是我想在不同的页面(分页)上显示我的数据,而不是在一个页面上。

我该怎么做?

更新

for (var i = 0; i < 100; i++) {
  $scope.lists.push('To do ' + i);
}

2 个答案:

答案 0 :(得分:0)

http://embed.plnkr.co/qKLWrClyYvyvGyIPLnLQ/preview上的简单示例。观察页面并构建要显示的数组

var app = angular.module('pagination', ['ui.bootstrap']);

app.controller('Example', function($scope){
  $scope.lists = [
    1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,15
  ];
  $scope.totalItems = $scope.lists.length;
  $scope.currentPage = 1;
  $scope.numPerPage = 8;
  $scope.theActualDisplayList = [];

  $scope.$watch('currentPage', function(page) {
    if($scope.currentPage) {
      var spliceFrom = ($scope.currentPage - 1) * $scope.numPerPage; 
      var offset = spliceFrom + $scope.numPerPage;
      $scope.theActualDisplayList = [];
      for (var i = spliceFrom; i < offset; i++) {
        if(i === $scope.lists.length) return false;
        $scope.theActualDisplayList.push($scope.lists[i]);
      }
    }
  });

});


<body ng-app="pagination" ng-controller="Example">
    <h1>Pagination.</h1>
    <ul><li ng-repeat="i in theActualDisplayList track by $index">{{i}}</li></ul>
    <pagination total-items="totalItems" ng-model="currentPage" max-size="5" boundary-links="true" items-per-page="numPerPage" class="pagination-sm"></pagination>
  </body>

答案 1 :(得分:0)

enter image description here 这里http://plnkr.co/edit/CcN7UPKW0EWgYDhuzHpN?p=preview我在使用angularjs面对动态分页的问题后做了一个plnkr,在这里我想你得到了所有修复。有关详细信息,您可以阅读我在http://tutexp.com/angularjs-datagrid-paging-sorting-filtering-using-asp-net-and-mssql/上撰写的文章 希望你能得到帮助。 如果我们在$ scope.Items

中包含项目列表,则在此处进行分页初始化
$scope.currentPage = 1; //current page
    $scope.entryLimit = 5; //max no of items to display in a page
    $scope.filteredItems = $scope.Items.length; //Initially for no filter
    $scope.totalItems = $scope.filteredItems;
    $scope.setPage = function (pageNo) {
        $scope.currentPage = pageNo;
    };
    $scope.filter = function () {
        $timeout(function () {
            $scope.filteredItems = $scope.filtered.length;
        }, 9000);
    };
    $scope.sort_by = function (predicate) {
        $scope.predicate = predicate;
        $scope.reverse = !$scope.reverse;
    };

在表格行中迭代列表项,如下所示

<tr ng-repeat="data in filtered = (Items  | filter:{'Name':SearchText}:false |
 filter:{'CountryId':  CountryId || undefined}:true |orderBy : predicate
:reverse) | startFrom:(currentPage-1)*entryLimit | limitTo:entryLimit">
       <td><b>{{data.Name }} </b></td>
                                            <td>
                                                <b>
                                                    {{data.CountryId == "1" ? 'Bangladesh' : ''}}
                                                    {{data.CountryId == "2" ? 'India' : ''}}
                                                    {{data.CountryId == "3" ? 'Pakistan' : ''}}
                                                    {{data.CountryId == "4" ? 'Vutan' : ''}}
                                                    {{data.CountryId == "5" ? 'Nepal' : ''}}
                                                </b>
                                            </td>
                                        </tr>