/* * Created with Sublime Text 3. * license: http://www.lovewebgames.com/jsmodule/index.html * User: 田想兵 * Date: 2015-06-11 * Time: 16:27:55 * Contact: 55342775@qq.com */ (function(root, factory) { //amd if (typeof define === 'function' && define.amd) { define(['$', 'query'], factory); } else if (typeof exports === 'object') { //umd module.exports = factory(); } else { root.Paging = factory(window.Zepto || window.jQuery || $, Query); } })(this, function($, Query) { $.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, current: $("#pageNum").val(), prevTpl: "이전", nextTpl: "다음", firstTpl: "처음", lastTpl: "마지막", ellipseTpl: "...", toolbar: false, hash:true }, 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); this.format(); }, bindEvent: function() { var _this = 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')) { _this.current = 1; } if ($(this).hasClass('js-page-prev')) { _this.current = Math.max(1, _this.current - 1); } if ($(this).hasClass('js-page-next')) { _this.current = Math.min(parseInt(_this.pagecount), parseInt(_this.current) + 1); } if ($(this).hasClass('js-page-last')) { _this.current = _this.pagecount; } } else if ($(this).data('page')) { _this.current = parseInt($(this).data('page')); } _this.go(); }); /* $(window).on('hashchange',function(){ var page= parseInt(Query.getHash('page')); if(_this.current !=page){ _this.go(page||1); } }) */ }, go: function(p) { var _this = this; this.current = p || this.current; this.current = Math.max(1, _this.current); this.current = Math.min(this.current, _this.pagecount); this.format(); if(this.settings.hash){ Query.setHash({ page:this.current }); } locationHref(this.current); this.settings.callback && this.settings.callback(this.current, this.pagesize, this.pagecount); }, changePagesize: function(ps) { this.render({ pagesize: ps }); }, format: function() { var html = ''; this.container.html(html); if (this.current == 1) { $('.js-page-prev', this.container).addClass('ui-pager-disabled'); $('.js-page-first', this.container).addClass('ui-pager-disabled'); } if (this.current == this.pagecount) { $('.js-page-next', this.container).addClass('ui-pager-disabled'); } this.container.find('li[data-page="' + this.current + '"]').addClass('focus').siblings().removeClass('focus'); if (this.settings.toolbar) { this.bindToolbar(); } }, bindToolbar: function() { var _this = this; var html = $('
  • 선택
  • '); var sel = $('.ui-select-pagesize', html); sel.val(this.pagesize); $('input', html).val(this.current); $('input', html).click(function() { $(this).select(); }).keydown(function(e) { if (e.keyCode == 13) { var current = parseInt($(this).val()) || 1; _this.go(current); } }); $('a', html).click(function() { var current = parseInt($(this).prev().val()) || 1; _this.go(current); }); sel.change(function() { _this.changePagesize($(this).val()); }); this.container.children('ul').append(html); } } return Paging; });