+function ($){
var Pagination = function(element,options){
this.$element = $(element);
this.options = $.extend({},Pagination.DEFAULTS,options);
this.init();
};
/*分页默认属性*/
Pagination.DEFAULTS = {
theme :'A',
jump: false,
message:'共条',
total: 0,
pageSize: 10,
pageSizeChanger: false,
current: 0,
display: [1, 4, 1],
size: '',
toPage: function() {}
};
/*分页初始化*/
Pagination.prototype.init = function() {
var options = this.options;
//1. 保存重要参数
//最多可显示页码数
this._maxPages = options.display[0] + options.display[1] + options.display[2];
//当前页码数
this._current = options.current;
//当前每页显示条数
this._pageSize = options.pageSize;
//2. 生成对应的DOM
this.createDom();
//3.设置分页总数和当前页码数
this.total(options.total, options.current);
//4. 初始化分页事件
this.initEvent();
};
/*生成DOM*/
Pagination.prototype.createDom = function() {
var options = this.options;
//1. 生成最外层结构
var pclass = 'mg-pagination theme' + options.theme + (options.small ? ' mg-small' : '');
this.$element.append(this.$pagination = $('
'));
this.$pageno = this.$pagination.find('[role="pageno"]');
//2. 生成对应的分页结构
if(options.theme === 'B') {
this.$pageno.append(
'
' +
'/9' +
'');
}else if(options.jump) {
this.$pageno.after('');
}
//3. 若有message,则添加message的DOM
if(options.message || options.pageSizeChanger) {
var message = options.message;
if(options.pageSizeChanger) {
message += message ? ',每页显示' : '每页显示';
}
this.$pagination.append(this.$message = $(''));
}
};
//设置总条目数
Pagination.prototype.total = function(total , current){
var $pagination = this.$pagination.show();
this._total = total = total && parseInt(total);
//若总数为0或不存在,分页隐藏
if(!total){
$pagination.hide();
return;
}
//1. 页面中为total填充数据
$pagination.find('[role="total"]').text(total);
//2. 获取总页码数
var pages = Math.ceil(total / this._pageSize);
$pagination.find('[role="pages"]').text(pages);
//3. 若总页数调整,则调整相应分页
if(pages !== this._pages) {
//保存页数
this._pages = pages;
//清空页码
this.$pageno.find('[role="no"]').remove();
}
//4. 设置当前页
this._current = current || 0;
this.current(this._current , true);
};
//调整当前页(当当前页 > 总页数时,将当前页设为最后一页)
Pagination.prototype.adjustCurrent = function(current){
current = parseInt(current) || 0;
current = current < this._pages ? current : this._pages - 1;
this._current = current;
return current;
};
//设置当前页
Pagination.prototype.current = function(current , init){
var $pageno = this.$pageno;
current = this.adjustCurrent(current);
if(this.options.theme === 'B') {
$pageno.find('input[type=text]').val(current + 1);
}else {
//1. 若无页码 或 总页数大于总显示页数,则生成分页
if(this._pages <= this._maxPages && $pageno.find('[role="no"]').length){
$pageno.find('.current').removeClass('current');
}else{
this.createPage(this.getDisplay(current) , current);
}
//2. 为页码加上当前状态
$pageno.find('[pageno="' + current + '"] .mg-pagination__no').addClass('current');
}
//3. 判断上一页、下一页是否可点
$pageno.find('.disabled').removeClass('disabled');
if(current === 0){
$pageno.find('[role="prev"] .mg-pagination__no, [role="first"] .mg-pagination__no').addClass('disabled');
}
if (current === this._pages - 1) {
$pageno.find('[role="next"] .mg-pagination__no, [role="last"] .mg-pagination__no').addClass('disabled');
}
!init && this.options.toPage.call(this , this._current , this._pageSize);
/*
var currentItem = this._current * this._pageSize + 1;
var ceilItem = currentItem + this._currentPer - 1 ;
ceilItem = ceilItem > this._totalRecorde ? this._totalRecorde : ceilItem;
this.$pagination.find('[role="per"]').text(currentItem + '-' + ceilItem);
!init && this.options.toPage.call(this , this._currentPage , this._currentPer);*/
};
//根据显示形式生成页码
Pagination.prototype.createPage = function(display , current){
var $ul = this.$pageno.empty();
$ul.append('');
//若display为纯数字,代表无省略号的出现
if(typeof display === 'number'){
for(var i = 0 , l = this._pages ; i < l ; i += 1){
var $li = $('');
$ul.append($li);
}
}else{
for(var j = 0 , jl = display.length ; j < jl ; j += 1){
var data = display[j];
if(data){
do{
var $li = $('');
$ul.append($li);
data[0] += 1;
}while(data[0] <= data[1]);
}else{
$ul.append('');
}
}
}
$ul.append('');
};
//获取当前页码下,分页的显示方式
Pagination.prototype.getDisplay = function(current){
var pages = this._pages; //获取总页数
if(this._maxPages >= pages) return pages;
var display = this.options.display;
var newDisplay = [];
//中间页的起始页和结尾页
var start = current - Math.floor((display[1] - 1)/2);
start = start < 0 ? 0 : start;
var end = current + Math.ceil((display[1] - 1) / 2);
end = end > pages ? pages : end;
//若中间显示页的最后一页小于前和中显示页数,即前无...
if(end < display[0] + display[1] ){
newDisplay = [[0,display[0] + display[1] - 1] , 0 ,[pages - display[2] , pages - 1]];
//若中间页
}else if(pages - start <= display[1] + display[2]){
newDisplay = [[0,display[0] - 1] , 0 , [pages - display[1] - display[2] , pages - 1]];
}else{
newDisplay = [[0 , display[0] - 1] , 0 ,
[start , end] , 0 ,
[pages - display[2] , pages - 1]
];
}
return newDisplay;
};
//分页事件
Pagination.prototype.initEvent = function(){
var self = this;
var $pagination = this.$pagination;
//前一页
$pagination.on('click.mg.page.prev' , '.no--prev:not(.disabled)' , function(){
self.current(self._current - 1);
});
//后一页
$pagination.on('click.mg.page.prev' , '.no--next:not(.disabled)' , function(){
self.current(self._current + 1);
});
//直接点击页
$pagination.on('click.mg.page.changeno' , '[role="no"]:not(.current) a' , function(){
self.current(parseInt($(this).parent('li').attr('pageno')));
});
//改变每页显示数
$pagination.on('change.mg.page.changeper' , 'select' , function(){
self.perPage($(this).val());
});
//跳转页
$pagination.on('click.mg.page.focus' , 'input[type=text]' , function(e){
$(this).select();
});
$pagination.on('keyup.mg.page.focus' , 'input[type=text]' , function(e){
if(e.keyCode === 13) {
jump($(this));
}
});
$pagination.on('click.mg.page.jump' , '.jump__btn' , function(){
var $input = $(this).parent().find('input');
jump($input);
});
function jump($input){
var value = parseInt($input.val());
if(value){
value = value - 1;
var temp = self._current;
value = self.adjustCurrent(value);
//若调整过的value和当前页码值不一样,则跳转页码
value !== temp && self.current(value);
}
self.options.theme !== 'B' && $input.val('');
}
};
//设置每页显示条数
Pagination.prototype.pageSize = function(pageSize){
/*
this.$pagination.find('[value="' + pageSize + '"]').attr('selected' , true);
var currentItem = this._currentPage * this._currentPer + 1;
this._currentPer = parseInt(per);
//设置本页显示
this.$pagination.find('[role="per"]').text(currentItem + '-' + (currentItem + this._currentPer));
this.options.perPage.fun.call(this , this._currentPer);
//保存总页数
this._totalPages = Math.ceil(this._totalRecorde / this._currentPer);
//设置当前页(改变本页显示后,当前页的第一条数据应为改变前的第一条数据)
this._currentPage = Math.ceil(currentItem / this._currentPer) - 1;
this.$pageno.find('[role="no"]').remove();
this.currentPage(this._currentPage);*/
};
function Plugin(option){
var args = Array.prototype.slice.call(arguments, 1);
var returnValue = this;
this.each(function(){
var $this = $(this),
data = $this.data('mg.pagination'),
options = typeof option === 'object' && option;
if(!data){
$this.data('mg.pagination',(data = new Pagination(this,options)));
}
if(typeof option === 'string'){
returnValue = data[option].apply(data, args) || returnValue;
}
});
return returnValue;
}
var old = $.fn.pagination;
$.fn.pagination = Plugin;
$.fn.pagination.Constructor = Pagination;
$.fn.pagination.noConflict = function(){
$.fn.pagination = old;
return this;
}
}(jQuery);