【问题标题】:AngularJS Pagination - Get Current Page and Pass to ControllerAngularJS 分页 - 获取当前页面并传递给控制器
【发布时间】: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-paginatedir-pagination-controls 指令。您能否更新问题以显示它们?
  • 已添加,当前页面似乎没有被传递。我创建了一个默认 angularUI 分页的示例,但似乎有同样的问题。

标签: javascript angularjs pagination


【解决方案1】:

问题是你正在分配:

var currPage = $scope.currentPage;

所以currPage 设置为1,因为你的控制器被实例化,然后永远不会改变。所以当你稍后在控制器中引用currPage 时,它仍然是1

您应该直接引用 $scope.currentPage 值,该值将由分页指令更新。

因此尝试将“更新”方法更改为:

$scope.update = function (articleSortOrder) {
    sortBy = articleSortOrder.value;
    getResultsPage($scope.currentPage, sortBy);
} 

这应该将正确的当前页面值传递给您的服务。

【讨论】:

  • 我一直在尝试,因为它应该可以工作......这就是奇怪的地方。它没有通过。也许它与将我的控制器作为 app.js 的一部分而不是在页面中调用有关? pageChanged 函数与表示正确页码的 newPage 变量一起使用。
  • 刚刚发现问题。我有一个控制器重叠,因为一个 div 被删除并关闭了该 div。把这算作累了的编码!谢谢@Michael Bromley
猜你喜欢
  • 2021-06-14
  • 2021-12-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-04
  • 2015-08-23
  • 2012-12-02
相关资源
最近更新 更多