JS分页控件,可用于无刷新分页
时间:2010-08-21 来源:杨琦
function PagerBar() {
this.PageIndex = 1;//页索引,当前页
this.PageSize = 10;//页面大小
this.PageCount = 0;//页总数
this.RecordCount = 0;//记录总数
this.ReplaceString = "《#PageLink》";//替换页数的文本,注:不可以有正则表达式中的符号
this.PreviouBarFormat = "";//上一页显示文本格式
this.NextFormat = ""; //下一页显示文本格式
this.PageFormat = ""; //页面连接显示文本格式
this.CurrentPageFormat = ""; //当前页显示文本格式
this.ShowPagesCount = 5;//显示页数量
this.GetPageString = function() {//输出HTML代码
var pagestr = "";
if (this.RecordCount % this.PageSize == 0) {//计算总也页数
this.PageCount = parseInt(this.RecordCount / this.PageSize);
}
else {
this.PageCount = parseInt(this.RecordCount / this.PageSize) + 1;
}
var regexp = new RegExp(this.ReplaceString, "g");//替换页数文本正则表达式
if (this.RecordCount > this.PageSize && this.PageIndex != 1) {//上一页HTML输出
pagestr += this.PreviouBarFormat.replace(regexp, this.PageIndex - 1);
}
var ShowPageFirest = this.PageIndex - parseInt(this.ShowPagesCount / 2 + 1) < 0 ? 0 : this.PageIndex - parseInt(this.ShowPagesCount / 2 + 1);//计算显示页数的其实页数
if (this.PageCount < this.ShowPagesCount) {//当也总数小于显示页数量
ShowPageFirest = 0;
}
else {
if (this.PageIndex > (this.PageCount - parseInt(this.ShowPagesCount / 2 + 1))) {//当页总数在后几页显示
ShowPageFirest = this.PageCount - this.ShowPagesCount;
}
}
for (var i = ShowPageFirest; i < ShowPageFirest + this.ShowPagesCount; i++) {//循环出书页数文本
if (this.PageIndex == i + 1) {
pagestr += this.CurrentPageFormat.replace(regexp, i + 1);
}
else {
pagestr += this.PageFormat.replace(regexp, i + 1);
}
if (i == this.PageCount - 1) {//当到达页总数的时候挑出循环
break;
}
}
if (this.RecordCount > this.PageSize && this.PageIndex != this.PageCount) {//输出下一页HTMl
pagestr += this.NextFormat.replace(regexp, this.PageIndex + 1);
}
return pagestr;
}
}