您的位置:首页 > Web前端 > JQuery

jquery 分页

2009-01-20 17:45 162 查看
jQuery.pagefoot = {
// 生成分页脚
create : function(_this, s) {
var pageCount = 0;
// 计算总页码
pageCount = (s.count / s.pagesize <= 0) ? 1 : (parseInt(s.count
/ s.pagesize) + ((s.count % s.pagesize > 0) ? 1 : 0));
s.current = (s.current > pageCount) ? pageCount : s.current;
// 循环生成页码
var strPage = "";
// 创建上一页
if (s.current <= 1) {
strPage += "<span class=/"disabled/">" + s.previous + "</span>";
} else {
strPage += "<a href=/"" + (s.current - 1) + "/">" + s.previous
+ "</a>";
}
// 开始的页码
var startP = 1;
// 页码左右显示最大页码数
var anyMore = parseInt(s.displaynum / 2, 10);
// 结束的页码
var endP = (s.current + anyMore) > pageCount ? pageCount : s.current
+ anyMore;

// 可显示的页码数(剩N个用来显示最后N页的页码)
var pCount = s.pagesize - s.displaylastNum;
if (s.current > (s.displaylastNum + anyMore)) {// 页码数太多时,则隐藏多余的页码
startP = s.current - anyMore;
for (var i = 1; i <= s.displaylastNum; i++) {
strPage += "<a href=/"" + i + "/">" + i + "</a>";
}
strPage += "...";
}
if (s.current + s.displaynum < pageCount) {// 页码数太多时,则隐藏前面多余的页码
endP = s.current + anyMore;
} else {
endP = pageCount;
}
for (i = startP; i <= endP; i++) {
if (s.current == i) {
strPage += "<span class=/"current/">" + i + "</span>";
} else {
strPage += "<a href=/"" + i + "/">" + i + "</a>";
}
}
if (s.current + s.displaynum < pageCount) {// 页码数太多时,则隐藏后面多余的页码
strPage += "...";
for (i = pageCount - s.displaylastNum + 1; i <= pageCount; i++) {
strPage += "<a href=/"" + i + "/">" + i + "</a>";
}
}
// 创建下一页
if (s.current >= pageCount) {
strPage += "<span class=/"disabled/">" + s.next + "</span>";
} else {
strPage += "<a href=/"" + (s.current + 1) + "/">" + s.next + "</a>";
}
$(_this).empty().append(strPage).find("a").click( function() {
// 得到翻页的页码
var ln = this.href.lastIndexOf("/");
var href = this.href;
var page = parseInt(href.substring(ln + 1, href.length));
s.current = page;
// 外部取消翻页时...
if (!$.pagefoot.paging(page, s.paging))
return false;

$.pagefoot.create(_this, s);
return false;
});
return this;
},
paging : function(page, callback) {
if (callback) {
if (callback(page) == false)
return false;
}
return true;
}
}

jQuery.fn.pagefoot = function(opt) {
/* 参数定义 */
var setting = {
pagesize :10 // 每页显示的页码数
,
count :0 // 数据条数
,
css :"mj_pagefoot" // 分页脚css样式类
,
current :1 // 当前页码
,
displaynum :7 // 中间显示页码数
,
displaylastNum :5 // 最后显示的页码数
,
previous :"上一页" // 上一页显示样式
,
next :"下一页" // 下一页显示样式
,
paging :null
// 分页事件触发时callback函数
};
opt = opt || {}
$.extend(setting, opt);
return this.each( function() {
$(this).addClass(setting.css);
$.pagefoot.create(this, setting);
});
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: