gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 04:41:13 25 4
gpt4 key购买 nike

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

Controller

$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);
}

最佳答案

关于 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>

关于javascript - 使用AngularJS如何解决分页错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27797216/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com