資料量不大、又不想為了分頁反覆請求是,可以在前端直接切顯示/隱藏。以下兩個範例分別處理表格與列表。
(function ($) {
$.fn.tablepage = function (oObj, dCountOfPage, fresh_id) {
var dPageIndex = 1;
var dNowIndex = 1;
var sPageStr = "";
var dCount = 0;
var oSource = $(this);
var nowIndex = 1;
change_page_content();
function change_page_content() {
// 取得資料筆數
dCount = oSource.children().children().length;
// 產生頁碼區
sPageStr = "<div class='ref'><a href='javascript:void(0)' id='fresh_"
+ fresh_id + "' onClick='fresh(this.id);'><span>重新整理</span></a></div>";
sPageStr += "<div class='msdn'><a href='javascript:void(0)'>首頁</a>"
+ "<a href='javascript:void(0)'>上一頁</a>"
+ "<a href='javascript:void(0)'>下一頁</a>"
+ "<a href='javascript:void(0)'>尾頁</a></div>";
sPageStr += "<div class='txt'>共" + Math.ceil(dCount / dCountOfPage)
+ "頁,當前第<strong>" + dNowIndex + "</strong>頁</div>";
oObj.html(sPageStr);
dPageIndex = 1;
// 過濾表格內容:只顯示當前頁範圍內的列
oSource.children().children("tr").each(function () {
if (dPageIndex >= (((dNowIndex - 1) * dCountOfPage) + 1) &&
dPageIndex <= (dNowIndex * dCountOfPage)) {
$(this).show();
} else {
$(this).hide();
}
dPageIndex++;
});
// 依目前位置停用首尾頁連結
var total = Math.ceil(dCount / dCountOfPage);
var $links = $("#table_page_" + fresh_id).children('.msdn').children();
if (dCount <= dCountOfPage) {
$links.each(function (i) { if (i < 4) $(this).addClass("disabled"); });
} else if (dNowIndex == total) {
$links.each(function (i) { if (i == 2 || i == 3) $(this).addClass("disabled"); });
} else if (dNowIndex == 1) {
$links.each(function (i) { if (i == 0 || i == 1) $(this).addClass("disabled"); });
}
// 綁定換頁事件
oObj.children().children().each(function () {
$(this).click(function () {
dNowIndex = $(this)[0].innerHTML;
if (dNowIndex == '首頁') {
dNowIndex = 1; change_page_content(); nowIndex = dNowIndex;
}
if (dNowIndex == '尾頁') {
dNowIndex = total; change_page_content(); nowIndex = dNowIndex;
}
if (dNowIndex == '下一頁' && nowIndex < total) {
dNowIndex = parseInt(nowIndex) + 1; change_page_content(); nowIndex = nowIndex + 1;
}
if (dNowIndex == '上一頁' && nowIndex > 1) {
dNowIndex = parseInt(nowIndex) - 1; change_page_content(); nowIndex = nowIndex - 1;
}
});
});
}
};
})(jQuery);
適合用來做橫向的標籤列,一次顯示五項:
<script type="text/javascript">
$(document).ready(function () {
var totalPage = $("ul li").length;
var current = 1;
if (totalPage > 5) {
$("ul li:gt(4)").hide();
$("#btnPrev")
.addClass("tabs-scroller-left-disabled").attr("disabled", "disabled")
.click(function () {
$("#btnNext").removeClass("tabs-scroller-right-disabled").removeAttr("disabled");
current -= 1;
$("ul li").show();
var start = current - 1;
var end = start + 4;
$("li:lt(" + start + "), li:gt(" + end + ")", $("ul")).hide();
if (current == 1) {
$(this).addClass("tabs-scroller-left-disabled").attr("disabled", "disabled");
}
});
$("#btnNext").click(function () {
$("#btnPrev").removeClass("tabs-scroller-left-disabled").removeAttr("disabled");
current += 1;
$("ul li").show();
var start = current - 1;
var end = current + 4 > totalPage - 1 ? totalPage - 1 : current + 3;
$("li:lt(" + start + "), li:gt(" + end + ")", $("ul")).hide();
if (current + 4 >= totalPage) {
$(this).addClass("tabs-scroller-right-disabled").attr("disabled", "disabled");
}
});
} else {
$("#btnPrev").addClass("tabs-scroller-left-disabled");
$("#btnNext").addClass("tabs-scroller-right-disabled");
}
});
</script>
用 innerHTML 當狀態判斷是原版最脆弱的地方。以按鈕顯示的文字("首頁"、"下一頁")決定行為,一旦介面文案一改,邏輯就壞了。應該用 data 屬性明確指定動作:
<a href="#" data-page="first">首頁</a>
<a href="#" data-page="prev">上一頁</a>
用 :gt() 與 :lt() 這類 jQuery 專屬選擇器在元素多時效能不佳,因為 jQuery 必須先選出全部元素再過濾。改用 slice() 或原生 CSS 的 nth-child 會好很多。
這個方案的前提是「全部資料都已經在畫面上」。當資料量到幾百筆以上,把所有 DOM 都建出來再隱藏,反而比請伺服器只回傳一頁更慢、更耗記憶體。
實務上的判斷標準:幾十筆以內用前端分頁沒問題;超過一百筆就該做真正的伺服器端分頁(用 LIMIT / OFFSET 或游標式分頁),前端只負責畫出目前這一頁與頁碼。此外,前端分頁對搜尋引擎不友善——所有內容都在同一個 HTML 裡,且隱藏的部分對 SEO 沒有加分。