角度1按钮而不是3

时间:2016-04-21 05:52:03

标签: javascript angularjs

我对此分页代码有疑问。我有1个按钮而不是3个。你能帮忙吗?

paginator.js

$scope.generateButtons = function () {
        var buttons = [],
            pageCount = getPageCount(),
            buttonCount;


        console.log("page " + pageCount);

        buttonCount = pageCount > 2 ? 3 : pageCount;

        for (var i = 0; i < buttonCount; i++) {
            var index = +$scope.offset + i -1;
            if (index > 0) {
                buttons.push(index);
            }
        };

        return buttons;
    };

View in plunker

2 个答案:

答案 0 :(得分:1)

我的建议是使用Angular UI bootstrap分页,而不是从头开始编写https://angular-ui.github.io/bootstrap/#/pagination

angular.module('ui.bootstrap.demo').controller('PaginationDemoCtrl', function ($scope, $log) {
  $scope.totalItems = 64;
  $scope.currentPage = 4;

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

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

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

答案 1 :(得分:0)

这与角度相关问题无关。关于$scope.generateButtons = function () {...}的逻辑,请根据需要改变逻辑。这是您的代码(已编辑),用于显示3个按钮。

    $scope.generateButtons = function () {
        var buttons = [],
            pageCount = getPageCount(),
            buttonCount;

        buttonCount = pageCount > 2 ? 3 : pageCount;

        for (var i = 0; i < buttonCount; i++) {
            var index = parseInt($scope.offset) + i+1;

            if (index >= 0) { // this `if` is not really needed
                buttons.push(index);
            }
        };

        return buttons;
    };

享受!