日期:2014-05-16  浏览次数:20522 次

javascript 实现table分页
function Page(iAbsolute,sTableId,sTBodyId,pageBar){
this.absolute = iAbsolute; //每页最大记录数
this.tableId = sTableId;
this.tBodyId = sTBodyId;
this.pageBar = pageBar;

//this.tFootId = sTFootId;
this.rowCount = 0;//记录数
this.pageCount = 0;//页数
this.pageIndex = 0;//页索引
this.__oTable__ = null;//表格引用
this.__oTBody__ = null;//要分页内容
this.__oTFoot__ = null;//要分页内容
this.__dataRows__ = 0;//记录行引用
this.__oldTBody__ = null;
this.__init__(); //初始化;
};
/**
* 初始化
*/
Page.prototype.__init__ = function(){
this.__oTable__ = document.getElementById(this.tableId);//获取table引用
this.__oTBody__ = this.__oTable__.tBodies[this.tBodyId];//获取tBody引用
//this.__oTFoot__= document.getElementById(this.tFootId)
this.__dataRows__ = this.__oTBody__.rows;
this.rowCount = this.__dataRows__.length;
try {
this.absolute = (this.absolute <= 0) || (this.absolute > this.rowCount) ? this.rowCount : this.absolute;
this.pageCount = parseInt(this.rowCount % this.absolute == 0 ? this.rowCount / this.absolute : this.rowCount / this.absolute + 1);
}catch(exception){
}
this.__updateTableRows__();
};

Page.prototype.getBar = function(obj){
var bar = document.getElementById(obj.id);
bar.innerHTML = "每页" + this.absolute + "条/共" + this.rowCount + "条";// 第2页/共6页 首页 上一页 1 2 3 4 5 6 下一页 末页
}
/**
* 下一页
*/
Page.prototype.nextPage = function() {
if(this.pageIndex + 1 < this.pageCount) {
this.pageIndex += 1;
this.__updateTableRows__();
}
//FY();
};
/**
* 上一页
*/
Page.prototype.prePage = function() {
if(this.pageIndex >= 1) {
this.pageIndex -= 1;
this.__updateTableRows__();
}
};
/**
* 首页
*/
Page.prototype.firstPage = function() {
if(this.pageIndex != 0) {
this.pageIndex = 0;
this.__updateTableRows__();
}
};
/**
* 尾页
*/
Page.prototype.lastPage = function() {
if(this.pageIndex + 1 != this.pageCount) {
this.pageIndex = this.pageCount - 1;
this.__updateTableRows__();
}
};
/**
* 页定位方法
*/
Page.prototype.aimPage = function(iPageIndex) {
if(iPageIndex > this.pageCount - 1) {
this.pageIndex = this.pageCount - 1;
}else if(iPageIndex < 0) {
this.pageIndex = 0;
}else {
this.pageIndex = iPageIndex;
}
this.__updateTableRows__();
};

/**
* 执行分页时,更新显示表格内容
*/
Page.prototype.__updateTableRows__ = function() {
var iCurrentRowCount = this.absolute * this.pageIndex;
var iMoreRow = this.absolute + iCurrentRowCount > this.rowCount ? this.absolute + iCurrentRowCount - this.rowCount : 0;
var tempRows = this.__cloneRows__();
var removedTBody = this.__oTable__.removeChild(this.__oTBody__);
var newTBody = document.createElement("tbody");
newTBody.setAttribute("id", this.tBodyId);

for(var i = iCurrentRowCount;i < this.absolute + iCurrentRowCount - iMoreRow;i++) {
newTBody.appendChild(tempRows[i]);
}
this.__oTable__.appendChild(newTBody);

//页脚显示分页
var pageBar = document.getElementById(this.pageBar);//分页工具栏
pageBar.innerHTML = "";

var leftBar = document.createElement("div"),cur;
leftBar.className = 'leftBar';
cur = this.pageIndex * this.absolute + ' 到 ' + (this.pageIndex + 1) * this.pageCount;
if(this.pageIndex === 0){
cur = ' 1 到 ' + this.absolute;
}
if(this.pageIndex === this.pageCount - 1){
cur = this.pageIndex * this.absolute + ' 到 ' + this.rowCount;
}
leftBar.innerHTML = "当前显示 " + cur + " 条,共 " + this.rowCount + " 条记录";
var rightBar = document.createElement('div');
rightBar.className = 'rightBar';
var btnPre = document.createElement("a");
var btnNext = document.createElement("a");

btnPre.className = 'button-left';
btnPre.title = '上一页';
btnPre.href = "javascript:page.prePage()";
btnPre.innerHTML = '<span class="prev"></span>';

rightBar.appendChild(btnPre);
btnNext.className = 'button-right';
btnNext.title = '下一页';
btnNext.href = "javascript:page.nextPage()";
btnNext.innerHTML = '<span class="next"></span>';
rightBar.appendChild(btnNext);

pageBar.appendChild(leftBar);
pageBar.appendChild(rightBar);

if(this.pageIndex == 0){
btnPre.disabled = "disabled";
btnPre.style.cursor = 'default';
}
if(this.pageCount - 1 ==