DirPaginate 可以满足这个问题的要求
请看这个Plunker http://plnkr.co/edit/xmjmIId0c9Glh5QH97xz?p=preview对于这个演示。
使用服务器端分页时,您必须获得此格式的 JSON 响应。
JSON 响应的格式
{
Count: 1400,
Items: [
{ // item 1... },
{ // item 2... },
{ // item 3... },
...
{ // item 25... }
]
}
您唯一需要注意的是您获得数据库中的项目总数,因为您需要它将其传递给我们的指令。
角度控制器的格式
.controller('UsersController', function($scope, $http) {
$scope.users = [];
$scope.totalUsers = 0;
$scope.usersPerPage = 25; // this should match however many results your API puts on one page
getResultsPage(1);
$scope.pagination = {
current: 1
};
$scope.pageChanged = function(newPage) {
getResultsPage(newPage);
};
function getResultsPage(pageNumber) {
// this is just an example, in reality this stuff should be in a service
$http.get('path/to/api/users?page=' + pageNumber)
.then(function(result) {
$scope.users = result.data.Items;
$scope.totalUsers = result.data.Count
});
}
})
最后,这是一个如何将其集成到 html 中的示例
HTML
<div ng-controller="UsersController">
<table>
<tr dir-paginate="user in users | itemsPerPage: usersPerPage" total-items="totalUsers" current-page="pagination.current">
<td>{{ user.name }}</td>
<td>{{ user.email }}</td>
</tr>
</table>
<dir-pagination-controls on-page-change="pageChanged(newPageNumber)"></dir-pagination-controls>
</div>
请参阅本页的使用异步数据部分。
https://github.com/michaelbromley/angularUtils/tree/master/src/directives/pagination#working-with-asynchronous-data https://github.com/michaelbromley/angularUtils/tree/master/src/directives/pagination#working-with-asynchronous-data