调用:
复制代码 代码如下:
var pagechange = function (index) {
var html = pager(divid, index, 5, 1000, pagechange, { showgoto: false, showfirst: false });
}
实现:
复制代码 代码如下:
pager = function (divpager, pageindex, pagesize, totalcount, pagechange, opt) {
var theopt = {
barsize: 5, //分页条显示的页码数
bartemplate: {bar} 共{totalpage}页{totalcount}条 {goto}, //显示模板
autohide: true, //是否自动隐藏
showfirst: true, //在totalpage>barsize时是自动否显示第一页链接
showlast: true, //在totalpage>barsize时是自动否显示最后一页链接
showgoto: true, //是否显示goto
autohidegoto: true //如果太少是否自动隐藏goto
};
if (opt) {
if (opt.barsize)
theopt.barsize = opt.barsize;
if (opt.bartemplate)
theopt.bartemplate = opt.bartemplate;
if (opt.autohide == false)
theopt.autohide = false;
if (opt.showfirst == false)
theopt.showfirst = false;
if (opt.showlast = false)
theopt.showlast = false;
if (opt.showgoto == false)
theopt.showgoto = false;
if (opt.autohidegoto == false)
theopt.autohidegoto = false;
}
var handles = window.mypagerchanges = (function (x) { return x; } (window.mypagerchanges || {}));
if (!mypagerchanges[divpager]) mypagerchanges[divpager] = pagechange;
var startpage = 0; //分页条起始页
var endpage = 0; //分页条终止页
var showfirst = true;
var showlast = true;
if (isnan(pageindex)) {
pageindex = 1;
}
pageindex = parseint(pageindex);
if (pageindex pageindex = 1;
if (pageindex * pagesize > totalcount) {
pageindex = math.ceil(totalcount / pagesize);
}
if (totalcount == 0) { //如果没数据
document.getelementbyid(divpager).innerhtml = ;
return ;
}
var totalpage = math.ceil(totalcount / pagesize);
if (theopt.autohide && totalcount document.getelementbyid(divpager).innerhtml = ;
return ;
}
if (totalpage startpage = 1;
endpage = this.totalpage;
theopt.showlast = theopt.showfirst = false;
}
else {
if (pageindex startpage = 1;
endpage = theopt.barsize;
theopt.showfirst = false;
}
else if (pageindex > (totalpage - theopt.barsize / 2)) { //最后几页时
startpage = totalpage - theopt.barsize + 1;
endpage = totalpage;
theopt.showlast = false;
}
else { //中间的页时
startpage = pageindex - math.ceil(theopt.barsize / 2) + 1;
endpage = pageindex + math.floor(theopt.barsize / 2);
}
if (totalpage theopt.showlast = theopt.showfirst = false;
}
}
function _getlink(index, txt) {
if (!txt) txt = index;
return + txt + ;
}
var barhtml = ; //分页条
barhtml += pageindex == 1 ? : _getlink(pageindex - 1, 上一页);
if (theopt.showfirst) {
barhtml += _getlink(1) + ...;
}
for (var index = startpage; index
if (index == pageindex) {
barhtml += + index + ;
}
else {
barhtml += _getlink(index);
}
}
if (theopt.showlast) {
barhtml += ... + _getlink(totalpage);
}
barhtml += pageindex == totalpage ? : _getlink(pageindex + 1, 下一页);
var gotohtml = ; //goto框及按钮
if (theopt.showgoto && theopt.bartemplate.indexof({goto}) > 0) {
if ((theopt.autohidegoto && totalpage > 15) || theopt.autohidegoto == false) {
var txtid = divpager + _goindex;
var indexval = document.getelementbyid(\ + txtid + \).value;
gotohtml += ;
gotohtml += ;
}
}
//替换模板
var pagerhtml = theopt.bartemplate.replace({bar}, barhtml)
.replace({totalcount}, totalcount)
.replace({pageindex}, pageindex)
.replace({totalpage}, totalpage)
.replace({goto}, gotohtml);
document.getelementbyid(divpager).innerhtml = pagerhtml;
return pagerhtml;
};
