【发布时间】:2015-04-04 11:02:58
【问题描述】:
我正在实现服务器端排序和分页,我需要传递用户所在的当前页面,以便如果他们排序并且位于与第一页不同的页面上(例如按“最少票数”排序在第 5 页上不会显示第 5 页上的第 1 页的重新排序结果,但显示应该在第 5 页上的重新排序结果)。基本上,我需要就地排序,但不知道如何获取当前页面。
服务器端寻呼工作没有问题,我相信我在这里遗漏了一些简单的东西。
HTML(请注意我使用的是这个自定义指令:https://github.com/michaelbromley/angularUtils/tree/master/src/directives/pagination)
<tr dir-paginate="article in articles | itemsPerPage:articlesPerPage" total-items="totalArticles" current-page="currentPage">
<td>
<div class="col-md-1 voting well">
<div class="votingButton" ng-click="upVote(articlevote);">
<i class="glyphicon glyphicon-chevron-up"></i>
</div>
<div class="badge badge-inverse">
<div>{{article.articlevotes}}</div>
</div>
<div class="votingButton" ng-click="downVote(articlevote);">
<i class="glyphicon glyphicon-chevron-down"></i>
</div>
</div>
</td>
<td>{{article.articletitle}}</td>
<td>{{article.articlecategoryid}}</td>
<td><a ng-href="#article/{{article.id}}/{{article.articlelink}}">{{article.articletitle}}</a></td>
</tr>
</table>
<dir-pagination-controls on-page-change="pageChanged(newPageNumber)"></dir-pagination-controls>
控制器
$scope.articles = [];
$scope.totalArticles = 0;
$scope.articlesPerPage = 10; // this should match however many results your API puts on one page
$scope.currentPage = 1;
// sort options
$scope.sortoptions = [
{
label: 'Most Votes',
value: 'articlevotes desc',
},
{
label: 'Least Votes',
value: 'articlevotes asc',
}
];
var sortBy = $scope.sortoptions[0].value;
var currPage = $scope.currentPage; // Get current page
console.log(currPage);
// Initial page load
getResultsPage(1, sortBy);
$scope.update = function (articleSortOrder) {
// get value of sort and log it
console.log(articleSortOrder.value);
sortBy = articleSortOrder.value;
// log current page and pass as parameter
console.log(currPage);
getResultsPage(currPage, sortBy); // need to make dynamic so it gets current page
}
$scope.pageChanged = function (newPage) {
getResultsPage(newPage, sortBy);
};
function getResultsPage(pageNumber, sortorder) {
// currently skipping by page number * articles per page
pfcArticles.query({ $skip: (pageNumber - 1) * $scope.articlesPerPage, $top: $scope.articlesPerPage, $orderby: sortorder, $inlinecount: 'allpages' }, function (data) {
$scope.articles = data.results;
$scope.totalArticles = data.count; // Can change to hard number to reduce total items instead of LimitTo
});
}
【问题讨论】:
-
如果我控制台记录 $scope.currentPage,它只返回数字 1。我已经尝试了一些分页插件中列出的代码,并且插件正确返回了页码,但我的没有.
-
我在您的视图代码中没有看到
dir-paginate和dir-pagination-controls指令。您能否更新问题以显示它们? -
已添加,当前页面似乎没有被传递。我创建了一个默认 angularUI 分页的示例,但似乎有同样的问题。
标签: javascript angularjs pagination