/**
* pagination分页插件
* @version 1.3.1
* @author mss
* @url http://maxiaoxiang.com/jquery-plugins/plugins/pagination.html
*
* @调用方法
* $(selector).pagination();
*/
; (function (factory) {
if (typeof define === "function" && (define.amd || define.cmd) && !jquery) {
// amd或cmd
define(["jquery"], factory);
} else if (typeof module === 'object' && module.exports) {
// node/commonjs
module.exports = function (root, jquery) {
if (jquery === undefined) {
if (typeof window !== 'undefined') {
jquery = require('jquery');
} else {
jquery = require('jquery')(root);
}
}
factory(jquery);
return jquery;
};
} else {
//browser globals
factory(jquery);
}
}(function ($) {
//配置参数
var defaults = {
totaldata: 0, //数据总条数
showdata: 0, //每页显示的条数
pagecount: 9, //总页数,默认为9
current: 1, //当前第几页
prevcls: 'pre', //上一页class
nextcls: '', //下一页class
prevcontent: '上一页', //上一页内容
nextcontent: '下一页', //下一页内容
normalcls: 'page', //普通页class
activecls: 'page on', //当前页选中状态
coping: true, //首页和尾页
ishide: true, //当前页数为0页或者1页时不显示分页
homepage: '', //首页节点内容
endpage: '', //尾页节点内容
keepshowpn: false, //是否一直显示上一页下一页
count: 3, //当前页前后分页个数
jump: false, //跳转到指定页数
jumpiptcls: 'jump-ipt', //文本框内容
jumpbtncls: 'jump-btn', //跳转按钮
jumpbtn: '跳转', //跳转按钮文本
callback: function () { } //回调
};
var pagination = function (element, options) {
//全局变量
var opts = options, //配置
current, //当前页
$document = $(document),
$obj = $(element); //容器
/**
* 设置总页数
* @param int page 页码
* @return opts.pagecount 总页数配置
*/
this.setpagecount = function (page) {
return opts.pagecount = page;
};
/**
* 获取总页数
* 如果配置了总条数和每页显示条数,将会自动计算总页数并略过总页数配置,反之
* @return int p 总页数
*/
this.getpagecount = function () {
return opts.totaldata || opts.showdata ? math.ceil(parseint(opts.totaldata) / opts.showdata) : opts.pagecount;
};
/**
* 获取总条数
/**
* 获取当前页
* @return int current 当前第几页
*/
this.getcurrent = function () {
return current;
};
/**
* 填充数据
* @param int index 页码
*/
this.filling = function (index) {
var html = '';
current = index || opts.current; //当前页码
var pagecount = this.getpagecount(); //获取的总页数
if (opts.coping) {
if (current > 1) {
html += ''+opts.homepage+'';
}
else {
html += '' + opts.homepage +'';
}
}
//html += "
";
if (opts.keepshowpn && current > 1) {//上一页
//html += '
';
html += '
' + opts.prevcontent+'';
} else {
if (opts.keepshowpn == false) {
$obj.find('.' + opts.prevcls) && $obj.find('.' + opts.prevcls).remove();
}
else {
html += '
' + opts.prevcontent +'';
}
}
//if (current >= opts.count + 2 && current != 1 && pagecount != opts.count) {
// var home = opts.coping && opts.homepage ? opts.homepage : '1';
// html += opts.coping ? '
...' : '';
//}
html += '
';
var end = current + opts.count;
var start = '';
//修复到最后一页时比第一页少显示两页
start = current === pagecount ? current - opts.count - 2 : current - opts.count;
((start > 1 && current < opts.count) || current == 1) && end++;
(current > pagecount - opts.count && current >= pagecount) && start++;
for (; start <= end; start++) {
if (start <= pagecount && start >= 1) {
if (start != current) {
html += '' + start + '';
} else {
html += '' + start + '';
}
}
}
html += '';
html += '
';
if (opts.keepshowpn && current < pagecount) {//下一页
html += '
' + opts. nextcontent+'';
} else {
if (opts.keepshowpn == false) {
$obj.find('.' + opts.nextcls) && $obj.find('.' + opts.nextcls).remove();
}
else {
html += '
' + opts.nextcontent +'';
}
}
if (opts.coping) {
if (parseint(current + 1) >= pagecount) {
html += '
' + opts.endpage +'';
}
else {
html += '
' + opts.endpage +'';
}
}
//html += "
";
// html += '共
' + pagecount + '条
';
if (opts.jump) {
html += '
';
}
$obj.empty().html(html);
};
//绑定事件
this.eventbind = function () {
var that = this;
var pagecount = that.getpagecount(); //总页数
var index = 1;
$obj.off().on('click', 'a', function () {
if ($(this).hasclass(opts.nextcls)) {
if ($obj.find('.' + opts.activecls).text() >= pagecount) {
$(this).addclass('disabled');
return false;
} else {
index = parseint($obj.find('.' + opts.activecls).text()) + 1;
}
} else if ($(this).hasclass(opts.prevcls)) {
if ($obj.find('.' + opts.activecls).text() <= 1) {
$(this).addclass('disabled');
return false;
} else {
index = parseint($obj.find('.' + opts.activecls).text()) - 1;
}
} else if ($(this).hasclass(opts.jumpbtncls)) {
if ($obj.find('.' + opts.jumpiptcls).val() !== '') {
index = parseint($obj.find('.' + opts.jumpiptcls).val());
} else {
return;
}
} else {
index = parseint($(this).data('page'));
}
that.filling(index);
typeof opts.callback === 'function' && opts.callback(that);
});
//输入跳转的页码
$obj.on('input propertychange', '.' + opts.jumpiptcls, function () {
var $this = $(this);
var val = $this.val();
var reg = /[^\d]/g;
if (reg.test(val)) {
$this.val(val.replace(reg, ''));
}
(parseint(val) > pagecount) && $this.val(pagecount);
if (parseint(val) === 0) {//最小值为1
$this.val(1);
}
});
//回车跳转指定页码
$document.keydown(function (e) {
if (e.keycode == 13 && $obj.find('.' + opts.jumpiptcls).val()) {
var index = parseint($obj.find('.' + opts.jumpiptcls).val());
that.filling(index);
typeof opts.callback === 'function' && opts.callback(that);
}
});
};
//初始化
this.init = function () {
this.filling(opts.current);
this.eventbind();
if (opts.ishide && this.getpagecount() == '1' || this.getpagecount() == '0') $obj.hide();
};
this.init();
};
$.fn.pagination = function (parameter, callback) {
if (typeof parameter == 'function') {//重载
callback = parameter;
parameter = {};
} else {
parameter = parameter || {};
callback = callback || function () { };
}
var options = $.extend({}, defaults, parameter);
return this.each(function () {
var pagination = new pagination(this, options);
callback(pagination);
});
};
}));