本文分享一個以原生 JavaScript 實作的表格分頁模組。核心思路是把 tbody 當成資料容器,每次換頁時重建 tbody 並只塞入當前頁該顯示的列,同時保留一份原始列的克隆,避免 DOM 移除後引用失效。
<script>
// 將 page 定義為全域變數,供下方事件使用
var page;
window.onload = function () {
page = new Page(5, 'table', 'sTBodyId'); // 每頁 5 筆
page.__init__();
page.__updateTableRows__();
};
// 以下方法皆定義在 window.onload 之外,否則執行會有問題
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; // 資料列集合
this.__oldTBody__ = null;
}
// 初始化
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;
try {
// 每頁筆數若 <= 0 或大於總筆數,則以總筆數為準
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) {}
};
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__();
};
// 依當前頁索引重繪 tbody
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__ 原本指向 this.__oTBody__,
移除 tbody 後該引用會失效,因此用克隆集合還原。 */
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;
};
</script>
<body>
<table width="100%" cellpadding="0" cellspacing="0" border="1">
<tr>
<td colspan="3" align="center">
每頁顯示 5 筆
<a href="javascript:void(0);" onclick="page.firstPage();return false;">首頁</a>
<a href="javascript:void(0);" onclick="page.prePage();return false;">上一頁</a>
<a href="javascript:void(0);" onclick="page.nextPage();return false;">下一頁</a>
<a href="javascript:void(0);" onclick="page.lastPage();return false;">尾頁</a>
</td>
</tr>
</table>
</body>
幾個實作上的重點值得留意:__cloneRows__ 之所以必要,是因為 tbody 一旦被移出 DOM,原本持有的 rows 集合就隨之失效;而總頁數用取餘判斷是否整除,是避免多出一頁空白頁的常見技巧。若要接上真實資料量,建議把分頁改到資料層(例如只向後端請求當前頁),並搭配 aimPage 做頁碼跳轉,效能會比在瀏覽器端反覆重建 DOM 好得多。