分页指令

//
//
//
'use strict';
//
angular.module('pagination', []).directive("pagination", function() {
    //
    return {
        restrict: 'EA',
        replace: true,
        scope: {
            option: '=pageOption',
            query: '&',
            pageable: '='
        },
        template: '',
        link: function($scope) {
            //
            $scope.option.$promise.then(function() {
                //
                //得到显示页数的数组
                $scope.page = getRange($scope.option.number, $scope.option.totalPages, 5);
                //
                $scope.isFirst = isFirst;
                //
                $scope.isLast = isLast;
            })

            //
            function isFirst() {
                return $scope.option.number === 0;
            }
            //
            function isLast() {
                return $scope.option.totalPages === 0 || $scope.option.number == $scope.option.totalPages - 1;
            }
            //
            $scope.pre = function() {
                $scope.pageClick('上一页');
            }
            $scope.next = function() {
                $scope.pageClick('下一页');
            }
            //绑定点击事件
            $scope.pageClick = function(page) {
                if (page == '上一页') {
                    if (isFirst()) {
                        return;
                    }
                    page = parseInt($scope.option.number) - 1;
                } else if (page == '下一页') {
                    if (isLast()) {
                        return;
                    }
                    page = parseInt($scope.option.number) + 1;
                } else {
                    page -= 1;
                }
                if (page < 1) {
                    page = 0;
                } else if (page > $scope.option.totalPages) {
                    page = $scope.option.totalPages - 1;
                }
                //点击相同的页数 不执行点击事件
                if (page == $scope.option.number) {
                    return;
                }
                //
                $scope.option.number = page;
                $scope.pageable.page = page;
                //
                $scope.query()
                //
                $scope.page = getRange($scope.option.number, $scope.option.totalPages, 5);
            }
            //返回页数范围(用来遍历)
            function getRange(curr, all, count) {
                //计算显示的页数
                curr = parseInt(curr) + 1;
                all = parseInt(all);
                count = parseInt(count);
                var from = curr - parseInt(count / 2);
                var to = curr + parseInt(count / 2) + (count % 2) - 1;
                //显示的页数容处理
                if (from <= 0) {
                    from = 1;
                    to = from + count - 1;
                    if (to > all) {
                        to = all;
                    }
                }
                if (to > all) {
                    to = all;
                    from = to - count + 1;
                    if (from <= 0) {
                        from = 1;
                    }
                }
                var range = [];
                for (var i = from; i <= to; i++) {
                    range.push(i);
                }
                return range;
            }
        }
    }
})

你可能感兴趣的:(分页指令)