angularJS分页

/**
 * name: public.pagination Version: 1.0.0 beta
 */
angular
        .module(‘public.pagination‘, [])
        .directive(
                ‘publicPagination‘,
                [ function() {
                    return {
                        restrict : ‘EA‘,
                        template : ‘<div class="page-list">‘
                                + ‘<ul class="pagination" ng-show="conf.totalItems > 0">‘
                                + ‘<li ng-class="{disabled: conf.currentPage == 1}" class="prevPage" ng-click="prevPage()"><span>&laquo;上一页 </span></li>‘
                                + ‘<li ng-repeat="item in pageList track by $index" ng-class="{active: item == conf.currentPage, separate: item == \‘...\‘}" ‘
                                + ‘ng-click="changeCurrentPage(item)">‘
                                + ‘<span>{{item}}</span>‘
                                + ‘</li>‘
                                + ‘<li ng-class="{disabled: conf.currentPage == conf.numberOfPages}"class="nextPage" ng-click="nextPage()"><span>下一页&raquo;</span></li>‘
                                + ‘</ul>‘
                                + ‘<div class="page-total" ng-show="conf.totalItems > 0">‘
                                + ‘每页&ensp;<select ng-model="conf.itemsPerPage" ng-options="option for option in conf.perPageOptions " ng-change="changeItemsPerPage()"></select>‘
                                + ‘&ensp;/共<strong>{{ conf.totalItems }}</strong>条&ensp;‘
                                + ‘跳转至&ensp;<input type="text" ng-model="jumpPageNum" ng-keyup="jumpPageKeyUp($event)"/>&ensp;页‘
                                + ‘</div>‘
                                + ‘<div class="no-items" ng-show="conf.totalItems <= 0">暂无数据</div>‘
                                + ‘</div>‘,
                        replace : true,
                        scope : {
                            conf : ‘=‘
                        },
                        link : function(scope, element, attrs) {
                            var conf = scope.conf;
                            var defaultPagesLength = 9;
                            var defaultPerPageOptions = [ 10, 15, 20, 30, 50 ];
                            var defaultPerPage = 15;
                            if (conf.pagesLength) {
                                conf.pagesLength = parseInt(conf.pagesLength,
                                        10);
                                if (!conf.pagesLength) {
                                    conf.pagesLength = defaultPagesLength;
                                }
                                if (conf.pagesLength % 2 === 0) {
                                    conf.pagesLength += 1;
                                }
                            } else {
                                conf.pagesLength = defaultPagesLength
                            }
                            if (!conf.perPageOptions) {
                                conf.perPageOptions = defaultPagesLength;
                            }
                            function getPagination(newValue, oldValue) {
                                if (conf.currentPage) {
                                    conf.currentPage = parseInt(
                                            scope.conf.currentPage, 10);
                                }
                                if (!conf.currentPage) {
                                    conf.currentPage = 1;
                                }
                                if (conf.totalItems) {
                                    conf.totalItems = parseInt(conf.totalItems,
                                            10);
                                }
                                if (!conf.totalItems) {
                                    conf.totalItems = 0;
                                    return;
                                }
                                if (conf.itemsPerPage) {
                                    conf.itemsPerPage = parseInt(
                                            conf.itemsPerPage, 10);
                                }
                                if (!conf.itemsPerPage) {
                                    conf.itemsPerPage = defaultPerPage;
                                }
                                conf.numberOfPages = Math.ceil(conf.totalItems
                                        / conf.itemsPerPage);
                                if (scope.conf.numberOfPages > 0
                                        && scope.conf.currentPage > scope.conf.numberOfPages) {
                                    scope.conf.currentPage = scope.conf.numberOfPages;
                                }
                                var perPageOptionsLength = scope.conf.perPageOptions.length;
                                var perPageOptionsStatus;
                                for (var i = 0; i < perPageOptionsLength; i++) {
                                    if (conf.perPageOptions[i] == conf.itemsPerPage) {
                                        perPageOptionsStatus = true;
                                    }
                                }
                                if (!perPageOptionsStatus) {
                                    conf.perPageOptions.push(conf.itemsPerPage);
                                }
                                conf.perPageOptions.sort(function(a, b) {
                                    return a - b
                                });
                                scope.pageList = [];
                                if (conf.numberOfPages <= conf.pagesLength) {
                                    for (i = 1; i <= conf.numberOfPages; i++) {
                                        scope.pageList.push(i);
                                    }
                                } else {
                                    var offset = (conf.pagesLength - 1) / 2;
                                    if (conf.currentPage <= offset) {
                                        for (i = 1; i <= offset + 1; i++) {
                                            scope.pageList.push(i);
                                        }
                                        scope.pageList.push(‘...‘);
                                        scope.pageList.push(conf.numberOfPages);
                                    } else if (conf.currentPage > conf.numberOfPages
                                            - offset) {
                                        scope.pageList.push(1);
                                        scope.pageList.push(‘...‘);
                                        for (i = offset + 1; i >= 1; i--) {
                                            scope.pageList
                                                    .push(conf.numberOfPages
                                                            - i);
                                        }
                                        scope.pageList.push(conf.numberOfPages);
                                    } else {
                                        scope.pageList.push(1);
                                        scope.pageList.push(‘...‘);
                                        for (i = Math.ceil(offset / 2); i >= 1; i--) {
                                            scope.pageList
                                                    .push(conf.currentPage - i);
                                        }
                                        scope.pageList.push(conf.currentPage);
                                        for (i = 1; i <= offset / 2; i++) {
                                            scope.pageList
                                                    .push(conf.currentPage + i);
                                        }
                                        scope.pageList.push(‘...‘);
                                        scope.pageList.push(conf.numberOfPages);
                                    }
                                }
                                scope.$parent.conf = conf;
                            }
                            scope.prevPage = function() {
                                if (conf.currentPage > 1) {
                                    conf.currentPage -= 1;
                                }
                                getPagination();
                                if (conf.onChange) {
                                    conf.onChange();
                                }
                            };
                            scope.nextPage = function() {
                                if (conf.currentPage < conf.numberOfPages) {
                                    conf.currentPage += 1;
                                }
                                getPagination();
                                if (conf.onChange) {
                                    conf.onChange();
                                }
                            };
                            scope.changeCurrentPage = function(item) {
                                if (item == ‘...‘) {
                                    return;
                                } else {
                                    conf.currentPage = item;
                                    getPagination();
                                    if (conf.onChange) {
                                        conf.onChange();
                                    }
                                }
                            };
                            scope.changeItemsPerPage = function() {
                                conf.currentPage = 1;
                                getPagination();
                                if (conf.onChange) {
                                    conf.onChange();
                                }
                            };
                            scope.jumpToPage = function() {
                                num = scope.jumpPageNum;
                                if (num.match(/\d+/)) {
                                    num = parseInt(num, 10);
                                    if (num && num != conf.currentPage) {
                                        if (num > conf.numberOfPages) {
                                            num = conf.numberOfPages;
                                        }
                                        conf.currentPage = num;
                                        getPagination();
                                        if (conf.onChange) {
                                            conf.onChange();
                                        }
                                        scope.jumpPageNum = ‘‘;
                                    }
                                }
                            };
                            scope.jumpPageKeyUp = function(e) {
                                var keycode = window.event ? e.keyCode
                                        : e.which;
                                if (keycode == 13) {
                                    scope.jumpToPage();
                                }
                            }
                            scope.$watch(‘conf.totalItems‘, function(value,
                                    oldValue) {
                                if (!value || value == oldValue) {
                                    if (conf.onChange) {
                                        conf.onChange();
                                        return;
                                    }
                                }
                                getPagination();
                            })
                        }
                    };
                } ]);

<div class="row text-center">
                    <public-pagination conf="paginationConf"></public-pagination>
                </div>

//分页
        $scope.paginationConf = {
            currentPage: 1,
            totalItems: 0,
            itemsPerPage: 5,
            pagesLength: 5,
            perPageOptions: [5, 10, 15, 20, 25],
            onChange: function(){
                gro = ($scope.paginationConf.currentPage-1)*($scope.paginationConf.itemsPerPage);
                    var CoreUrl = new coreUrl({"transCode":"B000112","indexNo":gro+1,"pageSize":$scope.paginationConf.itemsPerPage});
                    CoreUrl.$save(function(objc){
                        $scope.UserList = objc.rows;
                        page = objc.totalCount;
                      $scope.paginationConf.totalItems = page;
                    });    
            }
        };

perPageOptions控制默认每页显示条数

时间: 2024-07-30 22:48:23

angularJS分页的相关文章

angularjs 分页

<p>表格</p> <!-- 新 Bootstrap 核心 CSS 文件 --> <div id="divMain"> <table class="table table-bordered"> <tbody> <tr> <td>{{item.appName}}</td> <td>{{item.linkObjectName}}</td>

AngularJS分页实现

基本思路 一开始页码为1,Service向服务器端获取对应信息:点击上/下一页/跳转,通过对应的页码向服务器端获取对应的信息. 由于后台暂时没弄好,我实现的过程中直接读取准备好的JSON文件,通过页码获取对应的信息段并通过ng-repeat在页面显示具体信息. 展示 一开始直接在Controller中实现分页的功能代码 HTML部分 1 <div class="pagination"> 2 <ul> 3 <li class="pagination

angularjs 分页精华代码

//pageinfo$scope.pageSize=10;$scope.currentType={{ current_type }};$scope.currentPage={{ json_encode(current_page) }};$scope.totalPage={{ json_encode(total_page) }};$scope.pages = [];//分页数组var viewCount = 7;var firstIdx = ($scope.currentPage<=parseIn

AngularJS分页插件

1.angular.module('app',['mePagination']) .controller('myController',['$scope',function($scope){ $scope.myPage={ currentPage:1,//访问第几页数据,从1开始 totalItems:0,//数据库中总共有多少条数据 itemsPerPage: 30,//默认每页展示多少条数据,可更改 pagesLength: 15, perPageOptions: [10, 20, 30,

angular.js分页代码的实例

对于大多数web应用来说显示项目列表是一种很常见的任务.通常情况下,我们的数据会比较多,无法很好地显示在单个页面中.在这种情况下,我们需要把数据以页的方式来展示,同时带有转到上一页和下一页的功能.现在在学习angular,使用angularjs 分页,基于 directive 实现,样式使用的 bootstrap,直接在 html代码中加入 标签即可调用. 先来看下效果图 实例代码 1 app.directive('pagePagination', function(){ 2 return {

jsRender

有了jsRender 写在前面 说来也很巧, 下午再做一个页面,再普通不过的分页列表,我还是像往常一样,基于MVC环境下,我正常用PagedList.MVC AJAX做无刷新分页,这时候问题就来了,列表数据中有个轮播图用到了slides.js插件,轮播图也用到了分页,数据第一页轮播图页码能正常使用,数据列表翻到第二页则轮播图的页码就无法正常使用,实际上PagedList.MVC自带的样式文件已经和slides.j自带的样式文件冲突,我还特意修改了slides.js的样式文件,然并无卵用,让郁闷飞

有了jsRender,妈妈再也不用担心我用jq拼接DOM拼接的一团糟了、页面整齐了、其他伙伴读代码也不那么费劲了

写在前面 说来也很巧, 下午再做一个页面,再普通不过的分页列表,我还是像往常一样,基于MVC环境下,我正常用PagedList.MVC AJAX做无刷新分页,这时候问题就来了,列表数据中有个轮播图用到了slides.js插件,轮播图也用到了分页,数据第一页轮播图页码能正常使用,数据列表翻到第二页则轮播图的页码就无法正常使用,实际上PagedList.MVC自带的样式文件已经和slides.j自带的样式文件冲突,我还特意修改了slides.js的样式文件,然并无卵用,让郁闷飞一会... 1.基于M

基于Angularjs实现分页

基于Angularjs实现分页 http://www.cnblogs.com/sword-successful/archive/2015/06/28/4605222.html

Angularjs实现简单分页

一个后台中总需要一款分页,那我为了自己方便使用,实现如下效果 我把这个组件命名为tm.pagination,原因是因为起名真的太难起了.而且我网名也叫天名, TM就这样了吧.github地址https://github.com/miaoyaoyao/AngularJs-UI 分页在线查看 点击预览 http://demo.miaoyueyue.com/js/ng/AngularJs-UI/demo/pagination.html 分页兼容性 对不起,我不会去测试老掉牙的ie8浏览器.目前测试了i