(function() { $.fn.Paging = function(settings) { var arr = []; $(this).each(function() { var options = $.extend({ target: $(this) }, settings); var lz = new Paging(); lz.init(options); arr.push(lz); }); return arr; }; function Paging() { var rnd = Math.random().toString().replace('.', ''); this.id = 'paging_' + rnd; } Paging.prototype = { init: function(settings) { this.settings = $.extend({ callback: null, pagesize: 10, current: 1, prevTpl: "上一页", nextTpl: "下一页", firstTpl: "首页", lastTpl: "末页", ellipseTpl: "...", toolbar: true, hash: false, pageSizeList: [5, 10, 15, 20] }, settings); this.target = $(this.settings.target); this.container = $('
'); this.target.append(this.container); this.render(this.settings); this.format(); this.bindEvent(); }, render: function(ops) { this.count = ops.count || this.settings.count; this.pagesize = ops.pagesize || this.settings.pagesize; this.current = ops.current || this.settings.current; this.pagecount = Math.ceil(this.count / this.pagesize); if (ops.count === 0) { this.count = 0; this.pagecount = 0; this.current = 0; } this.format(); }, bindEvent: function() { var me = this; this.container.on('click', 'li.js-page-action, li.ui-pager', function(e) { if ($(this).hasClass('ui-pager-disabled') || $(this).hasClass('focus')) { return false; } if ($(this).hasClass('js-page-action')) { if ($(this).hasClass('js-page-first')) { me.current = 1; } if ($(this).hasClass('js-page-prev')) { me.current = Math.max(1, me.current - 1); } if ($(this).hasClass('js-page-next')) { me.current = Math.min(me.pagecount, me.current + 1); } if ($(this).hasClass('js-page-last')) { me.current = me.pagecount; } } else if ($(this).data('page')) { me.current = parseInt($(this).data('page')); } me.go(); }); }, go: function(p) { var me = this; this.current = p || this.current; this.current = Math.max(1, me.current); this.current = Math.min(this.current, me.pagecount); this.format(); if(this.settings.hash) { Query.setHash({ page:this.current }); } this.settings.callback && this.settings.callback(this.current, this.pagesize, this.pagecount); }, changePagesize: function(ps) { this.render({ pagesize: ps }); this.settings.callback && this.settings.callback(this.current, this.pagesize, this.pagecount); }, format: function() { var html = '