Angularjs Pagination Service

A pagination service which build by angularjs and bootstrap css

by Tony Guo

HTML

<script src="https://code.angularjs.org/1.4.8/angular.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<div ng-controller="myDemoCtrl">

 <pagination paging="paging"></pagination>
</div>

JavaScript

var customPagination = angular.module('customPagination', []);
 customPagination.factory('Pagination', function () {
        /**
        *	constructor
        *   @param {Number} total The total data row counts
        *	@param {Object} pagination Several parameters for paging range or limit etc.. this can be over write and append more custom params inside. For details, see property 'opts' of Pagination.
        */
        function Pagination(total, pagination) {
            this.total = (!total || total < 0) ? 1 : total;
            angular.extend(this.opts, pagination || {});
        }
        Pagination.prototype = {
            //Initialize options with default values
            opts: {
                //Current page index
                currentPage: 1,
                //Total page numbers which catulated by total and pageSize    
                totalPage: 1,
                //Rows in every page
                pageSize: 10,
                //Current selected page index
                currSelectedPage: 1,
                //maxmium page number
                visiblePages: 7,
                //callback function, invokes while clicking page number
                //@param {PageIndex} delivers the triggered page index.
                renderData: function () {
                    return false;

                }
            },
            load: function () {
                //generate page links
                var pages = [];
                var opts = this.opts;
                opts.totalPage = Math.ceil(this.total / opts.pageSize);
                opts.endPage = opts.totalPage;
                var half = Math.floor(opts.visiblePages / 2);
                var start = opts.currentPage - half + 1 - opts.visiblePages % 2;
                var end = opts.currentPage + half;

                // handle boundary case
                if (start <= 0) {
                    start = 1;
                    end = opts.visiblePages;
                }
                if...