gpt4 book ai didi

angularjs - 如何告诉 ui-Bootstrap 对哪些内容进行分页?

转载 作者:行者123 更新时间:2023-12-02 09:11:01 25 4
gpt4 key购买 nike

我正在使用 ui-Bootstrap,我正在尝试让分页正常工作,但我似乎遗漏了一些东西。我已经阅读了文档并查看了一堆笨蛋,试图弄清楚他们如何指定要分页的内容,但我没有运气。

这是我所做的http://plnkr.co/edit/5mfiAcOaGw8z8VinhIQo?p=preview

<section class="main" ng-controller="contentCtrl">
<div ng-repeat="friend in friends">
{{friend.name}}
</div>
<pagination total-items="totalItems" items-per-page="itemsPerPage" ng-model="currentPage" ng-change="pageChanged()"></pagination>

<p>
total Items: {{totalItems}}<br />
Items per page: {{itemsPerPage}}<br />
Current Page: {{currentPage}}
</p>
</section>

Controller :

angular.module('plunker', ['ui.bootstrap'])
.controller('contentCtrl', function ($scope) {

$scope.friends = [
{'name':'Jack'},
{'name':'Tim'},
{'name':'Stuart'},
{'name':'Richard'},
{'name':'Tom'},
{'name':'Frank'},
{'name':'Ted'},
{'name':'Michael'},
{'name':'Albert'},
{'name':'Tobby'},
{'name':'Mick'},
{'name':'Nicholas'},
{'name':'Jesse'},
{'name':'Lex'},
{'name':'Robbie'},
{'name':'Jake'},
{'name':'Levi'},
{'name':'Edward'},
{'name':'Neil'},
{'name':'Hugh'},
{'name':'Hugo'},
{'name':'Yanick'},
{'name':'Matt'},
{'name':'Andrew'},
{'name':'Charles'},
{'name':'Oliver'},
{'name':'Robin'},
{'name':'Harry'},
{'name':'James'},
{'name':'Kelvin'},
{'name':'David'},
{'name':'Paul'}
];

$scope.totalItems = 64;
$scope.itemsPerPage = 10
$scope.currentPage = 1;

$scope.setPage = function (pageNo) {
$scope.currentPage = pageNo;
};

$scope.pageChanged = function() {
console.log('Page changed to: ' + $scope.currentPage);
};

$scope.maxSize = 5;
$scope.bigTotalItems = 175;
$scope.bigCurrentPage = 1;
});

最佳答案

我可以简单地添加以下引用:

  1. bootstrap-css
  2. Angular.js
  3. Angular-ui-bootstrap

你的 body 可能看起来像这样:

<html ng-app="friends"> 
<head>
...
</head>
<body>
<h4>Paginated Friends</h4>
<section class="main" ng-controller="contentCtrl">
<div ng-repeat="friend in filteredFriends">
{{friend.name}}
</div>
<pagination total-items="totalItems" items-per-page="itemsPerPage"
ng-model="currentPage" ng-change="pageChanged()"></pagination>
<p>
Total items: {{totalItems}}<br />
Items per page: {{itemsPerPage}}<br />
Current Page: {{currentPage}}
</p>
</section>
</body>
</html>

然后定义以下 Controller :

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

app.factory('friendsFactory', function($resource) {
return $resource('friends.json');
});

app.controller('contentCtrl', function ($scope, friendsFactory) {
$scope.friends = friendsFactory.query();

$scope.itemsPerPage = 10
$scope.currentPage = 1;

// $scope.maxSize = 5;
// $scope.bigTotalItems = 175;
// $scope.bigCurrentPage = 1;

$scope.pageCount = function () {
return Math.ceil($scope.friends.length / $scope.itemsPerPage);
};

$scope.friends.$promise.then(function () {
$scope.totalItems = $scope.friends.length;
$scope.$watch('currentPage + itemsPerPage', function() {
var begin = (($scope.currentPage - 1) * $scope.itemsPerPage),
end = begin + $scope.itemsPerPage;

$scope.filteredFriends = $scope.friends.slice(begin, end);
});
});
});

关于angularjs - 如何告诉 ui-Bootstrap 对哪些内容进行分页?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23653427/

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