DIV CSS 佈局教程網

JavaScript 實現表格分頁功能的完整方法
編輯:JavaScript綜合知識     

本文將分享如何使用 JavaScript 實現表格的分頁功能,透過封裝一個 Page 物件來管理每頁顯示的資料筆數、頁碼切換與表格內容更新,具有實用價值。

實作思路

分頁的核心是把表格資料依每頁筆數切分,並提供首頁、上一頁、下一頁、尾頁與頁碼定位等操作。這裡以一個 Page 構造函數封裝相關狀態與方法,透過操作 tbody 的節點來實現每頁內容的動態更新。

核心程式碼

//定義 page 為全域變數
var page;
window.onload = function() {
  var table = document.getElementById("table");
  //定義每頁顯示的筆數並初始化
  page = new Page(5, 'table', 'sTBodyId');
  page.__init__();
  page.__updateTableRows__();
}
function Page(iAbsolute, sTableId, sTBodyId) {
  this.absolute = iAbsolute; //每頁顯示資料筆數
  this.tableId = sTableId;
  this.tBodyId = sTBodyId;
  this.rowCount = 0; //記錄數
  this.pageCount = 0; //頁數
  this.pageIndex = 0; //頁索引
  this.__oTable__ = null; //表格引用
  this.__oTBody__ = null; //要分頁的內容
  this.__dataRows__ = 0; //記錄行引用
}
//初始化
Page.prototype.__init__ = function() {
  this.__oTable__ = document.getElementById(this.tableId);
  this.__oTBody__ = this.__oTable__.tBodies[this.tBodyId];
  this.__dataRows__ = this.__oTBody__.rows;
  this.rowCount = this.__dataRows__.length;
  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);
}
//下一頁
Page.prototype.nextPage = function() {
  if (this.pageIndex + 1 < this.pageCount) { this.pageIndex += 1; this.__updateTableRows__(); }
}
//上一頁
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);
  this.__dataRows__ = tempRows;
  this.__oTBody__ = newTBody;
}
//克隆原始操作行集合
Page.prototype.__cloneRows__ = function() {
  var tempRows = [];
  for (var i = 0; i < this.__dataRows__.length; i++) {
    tempRows[i] = this.__dataRows__[i].cloneNode(1);
  }
  return tempRows;
}

頁面中的分頁連結透過 onclick 呼叫 page.firstPage()、page.prePage()、page.nextPage()、page.lastPage() 等方法即可驅動。透過 cloneNode 複製原始資料行、再以 removeChild 與 appendChild 重建 tbody,就能實現流暢的分頁切換。希望本文所述的做法能對大家的 JavaScript 程式設計有所幫助。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved