首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >角度分页,停止分页

角度分页,停止分页
EN

Stack Overflow用户
提问于 2016-04-19 20:09:36
回答 2查看 393关注 0票数 1

嗨,我有一个保留的问题,在分页的尖锐湿页。我的意思是阻止分页1,但它没有显示为0。需要找到一个条件吗?

代码语言:javascript
复制
$scope.setPage = function (index) {
        $scope.items.paging.offset = index;
        console.log(index);
    };

all projekt in Plunker

EN

回答 2

Stack Overflow用户

发布于 2016-04-19 21:49:46

paginator.html

以下代码:

代码语言:javascript
复制
<ul class="pagination">
    <li ng-repeat="index in [getSelectedPage() - 1 , getSelectedPage(), getSelectedPage() + 1]"
        ng-click = "setPage(index)">
        <a href="#" class="btn btn-primary">{{ index + 1  }}</a>
    </li>
</ul>

可以重新设计成类似这样的东西:

代码语言:javascript
复制
<ul class="pagination">
    <li ng-repeat="index in getPaginationButtons()"
        ng-click = "setPage(index)">
        <a href="#" class="btn btn-primary">{{ index + 1  }}</a>
    </li>
</ul>

paginator.js

可以添加以下代码:

代码语言:javascript
复制
// but IMHO it's not very good to hardcode 3 buttons
// and not to check a number of available pages
$scope.getPaginationButtons = function () {
    var buttons = [];

    // TODO: fix offset or set it to 0
    // if $scope.items.paging.offset is out of valid range
    // for example, someone removes all items
    // from another tab or window or browser
    // and then you click on the page offset
    // which is not present

    if ($scope.items.paging.offset > 0) {
      buttons.push($scope.items.paging.offset - 1);
    }

    buttons.push($scope.items.paging.offset);

    // TODO: do the following only if the next page is present
    buttons.push($scope.items.paging.offset + 1);

    return buttons;
};
票数 0
EN

Stack Overflow用户

发布于 2016-04-25 04:27:41

我想下面的部分可以在suggested code中改进

代码语言:javascript
复制
if ($scope.items.paging.offset > 0) {
  buttons.push($scope.items.paging.offset - 1);
}

buttons.push($scope.items.paging.offset);

// TODO: do the following only if the next page is present
buttons.push($scope.items.paging.offset + 1);

并应替换为以下内容:

代码语言:javascript
复制
// if not a first page then add a number of previous one
if ($scope.offset > 0) {
  buttons.push($scope.offset / $scope.limit - 1);
}

buttons.push($scope.offset / $scope.limit);

// if there are more pages then show button of the next one
if (($scope.offset + $scope.limit) < $scope.total) {
  buttons.push($scope.offset / $scope.limit + 1);
}

以及你最新的柱塞示例中的以下代码(例如,来自https://plnkr.co/edit/N8HZ6qCtsj0fF4YMULFW):

代码语言:javascript
复制
    function getPageCount() {
        return Math.ceil($scope.total / $scope.limit);
    }

应该被弃用/删除。

并且下面的页面索引的偏移量的计算应该使用IMHO:

代码语言:javascript
复制
    $scope.setPage = function (index) {
        $scope.offset = index * $scope.limit;
    };
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36717949

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档