;(function ($, window, document, undefined) {
'use strict';
function Paging(element, options) {
this.element = element;
this.options = {
pageNum: options.pageNum || 1, // 当前页码
totalNum: options.totalNum, // 总页码
totalList: options.totalList, // 数据总记录
preCount: options.preCount,
callback: options.callback // 回调函数\
};
this.init();
}
Paging.prototype = {
constructor: Paging,
init: function () {
this.createHtml();
this.bindEvent();
},
createHtml: function () {
var me = this;
var content = [];
var pageNum = me.options.pageNum;
var totalNum = me.options.totalNum;
var totalList = me.options.totalList;
var preCount = me.options.preCount;
content.push(" ");
// 总页数大于6必显示省略号
if (totalNum > 6) {
// 1、当前页码小于5且总页码大于6 省略号显示后面+总页码
if (pageNum < 5) {
// 1与6主要看要显示多少个按钮 目前都显示5个
for (var i = 1; i < 6; i++) {
if (pageNum !== i) {
content.push("");
} else {
content.push("");
}
}
content.push(". . .");
content.push("");
} else {
// 2、当前页码接近后面 到最后页码隔3个 省略号显示后面+总页面
if (pageNum < totalNum - 3) {
for (var i = pageNum - 2; i < pageNum + 3; i++) {
if (pageNum !== i) {
content.push("");
} else {
content.push("");
}
}
content.push(". . .");
content.push("");
} else {
// 3、页码至少在5,最多在【totalNum - 3】的中间位置 第一页+省略号显示前面
content.push("");
content.push(". . .");
for (var i = totalNum - 4; i < totalNum + 1; i++) {
if (pageNum !== i) {
content.push("");
} else {
content.push("");
}
}
}
}
} else {
// 总页数小于6
for (var i = 1; i < totalNum + 1; i++) {
if (pageNum !== i) {
content.push("");
} else {
content.push("");
}
}
}
content.push("");
// content.push("第/"+totalNum+"");
me.element.html(content.join(''));
// DOM重新生成后每次调用是否禁用button
setTimeout(function () {
me.dis();
}, 20);
},
bindEvent: function () {
var me = this;
me.element.off('click', 'button');
// 委托新生成的dom监听事件
me.element.on('click', 'button', function () {
var id = $(this).attr('id');
var num = parseInt($(this).html());
var pageNum = me.options.pageNum;
if (id === 'prePage') {
if (pageNum !== 1) {
me.options.pageNum -= 1;
}
} else if (id === 'nextPage') {
if (pageNum !== me.options.totalNum) {
me.options.pageNum += 1;
}
} else if (id === 'firstPage') {
if (pageNum !== 1) {
me.options.pageNum = 1;
}
} else if (id === 'lastPage') {
if (pageNum !== me.options.totalNum) {
me.options.pageNum = me.options.totalNum;
}
}else if (id === 'jump') {
pageNum=parseInt($("#count").val());
if (pageNum>=1&&pageNum<=me.options.totalNum) {
me.options.pageNum =pageNum
}else{
alert("请输入正确页面")
return;
}
} else {
me.options.pageNum = num;
}
me.createHtml();
if (me.options.callback) {
me.options.callback(me.options.pageNum);
}
});
},
dis: function () {
var me = this;
var pageNum = me.options.pageNum;
var totalNum = me.options.totalNum;
if (pageNum === 1) {
me.element.children('#firstPage, #prePage').prop('disabled', true);
}
if (pageNum === totalNum) {
me.element.children('#lastPage, #nextPage').prop('disabled', true);
}
}
};
$.fn.paging = function (options) {
return new Paging($(this), options);
}
})(jQuery, window, document);