//分页插件
(function ($) {
var ms = {
init: function (obj, args) {
return (function () {
ms.fillhtml(obj, args);
ms.bindevent(obj, args);
})();
},
//填充html
fillhtml: function (obj, args) {
return (function () {
obj.empty();
if (args.totalcount > 0)
obj.append('共' + args.totalcount + '条信息,' + args.pagecount + '页 ');
//上一页
if (args.current > 1) {
obj.append('上一页');
} else {
obj.remove('.prevpage');
obj.append('上一页');
}
//中间页码
//if (args.current != 1 && args.current >= args.showcount && args.pagecount != args.showcount) {
// obj.append('' + 1 + '');
//}
if (args.current != 1 && args.current >= 4 && args.pagecount != 4) {
obj.append('' + 1 + '');
}
if (args.current - 2 > 2 && args.current <= args.pagecount && args.pagecount > 5) {
obj.append('...');
}
var start = args.current - 2, end = args.current + 2;
if ((start > 1 && args.current < 4) || args.current == 1) {
end++;
}
if (args.current > args.pagecount - 4 && args.current >= args.pagecount) {
start--;
}
for (; start <= end; start++) {
if (start <= args.pagecount && start >= 1) {
if (start != args.current) {
obj.append('' + start + '');
} else {
obj.append('' + start + '');
}
}
}
if (args.current + 2 < args.pagecount - 1 && args.current >= 1 && args.pagecount > 5) {
obj.append('...');
}
if (args.current != args.pagecount && args.current < args.pagecount - 2 && args.pagecount != 4) {
obj.append('' + args.pagecount + '');
}
//下一页
if (args.current < args.pagecount) {
obj.append('下一页');
} else {
obj.remove('.nextpage');
obj.append('下一页');
}
})();
},
//绑定事件
bindevent: function (obj, args) {
return (function () {
obj.on("click", "a.tcdnumber", function () {
var current = parseint($(this).text());
ms.fillhtml(obj, { "current": current, "pagecount": args.pagecount, "showcount": args.showcount });
if (typeof (args.backfn) == "function") {
args.backfn(current);
}
});
//上一页
obj.on("click", "a.prevpage", function () {
var current = parseint(obj.children("a.current").text());
ms.fillhtml(obj, { "current": current - 1, "pagecount": args.pagecount, "showcount": args.showcount });
if (typeof (args.backfn) == "function") {
args.backfn(current - 1);
}
});
//下一页
obj.on("click", "a.nextpage", function () {
var current = parseint(obj.children("a.current").text());
ms.fillhtml(obj, { "current": current + 1, "pagecount": args.pagecount, "showcount": args.showcount });
if (typeof (args.backfn) == "function") {
args.backfn(current + 1);
}
});
})();
}
}
$.fn.createpage = function (options) {
var args = $.extend({
totalcount: 0,
pagecount: 10,
current: 1,
showcount: 5,
backfn: function () { }
}, options);
ms.init(this, args);
}
})($);