DIV CSS 佈局教程網

JavaScript 分頁控制項:可搭配無刷新分頁使用
編輯:JavaScript綜合知識     

這是一個以建構函式寫成的分頁控制項,把「頁碼 HTML 的產生」整段封裝起來,讓畫面呈現與資料邏輯分離。使用方式還需要搭配自己的資料載入流程,但裡頭的函式組織方式值得參考。

建構與參數

function PagerBar(recordcount, pagesize, pageindex, showpagecount) {
  var NumberRegex = new RegExp(/^\d+$/);
  this.PageIndex = 1;   // 頁索引,當前頁
  if (pageindex != null && NumberRegex.test(pageindex)) this.PageIndex = parseInt(pageindex);

  this.PageSize = 10;   // 每頁筆數
  if (pagesize != null && NumberRegex.test(pagesize)) this.PageSize = parseInt(pagesize);

  this.RecordCount = 0; // 記錄總數
  if (recordcount != null && NumberRegex.test(recordcount)) this.RecordCount = parseInt(recordcount);

  this.PageCount = 0;   // 頁總數
  var PagerBar = this;

四個參數都有型別檢查,傳入非數字時會退回預設值,避免後續計算出 NaN。

總頁數計算

  function CalculatePageCount(_pagesize, _recordcount) {
    if (_pagesize != null && NumberRegex.test(_pagesize)) PagerBar.PageSize = parseInt(_pagesize);
    if (_recordcount != null && NumberRegex.test(_recordcount)) PagerBar.RecordCount = parseInt(_recordcount);
    else PagerBar.RecordCount = 0;

    if (PagerBar.RecordCount % PagerBar.PageSize == 0) {
      PagerBar.PageCount = parseInt(PagerBar.RecordCount / PagerBar.PageSize);
    } else {
      PagerBar.PageCount = parseInt(PagerBar.RecordCount / PagerBar.PageSize) + 1;
    }
  }

  if (this.RecordCount != 0) {
    CalculatePageCount(this.PageSize, this.RecordCount);
  }

可設定的顯示格式

這個控制項最大的特色,是把每一種頁碼元素都開成可自訂的屬性,並用一個個 IsShowXxx 布林值控制要不要顯示:

  this.ReplaceString = "《#PageLink》";  // 頁數的佔位符,不可含正則特殊字元
  this.ShowPagesCount = 5;               // 顯示幾個頁碼
  if (showpagecount != null && NumberRegex.test(showpagecount.toString()))
    this.ShowPagesCount = parseInt(showpagecount);

  this.PreviouBarFormat = "";            // 上一頁格式
  this.IsShowPreviouString = true;       // 是否顯示上一頁
  this.NextBarFormat = "";               // 下一頁格式
  this.IsShowNextString = true;
  this.PageBarFormat = "";               // 一般頁碼連結格式
  this.CurrentBarFormat = "";            // 當前頁格式
  this.IsShowPageString = true;
  this.FristBarFormat = "";              // 首頁格式
  this.IsShowFristString = true;
  this.LastBarFormat = "";               // 尾頁格式
  this.IsShowLastString = true;
  this.CurrentRecordBarFormat = "當前記錄{0}-{1}";  // 目前記錄範圍
  this.IsShowCurrentRecordString = true;
  this.CurrentPageBarFormat = "當前第" + this.ReplaceString + "頁,共" +
    (this.PageCount == 0 ? 1 : this.PageCount) + "頁";
  this.IsShowCurrentPageString = true;
  this.OtherBarFormat = "";              // 其他頁(如 ... 跳段)
  this.IsShowOtherString = true;

  var regexp = new RegExp(this.ReplaceString, "g");  // 頁數替換用的正則

各段 HTML 的產生函式

內部為每一種元素各寫一個 getter,共同邏輯都是「把格式字串中的佔位符換成實際頁碼」:

  function GetFristPageString() {   // 首頁
    if (PagerBar.FristBarFormat != "" && PagerBar.PageIndex != 1) {
      return PagerBar.FristBarFormat.replace(regexp, 1);
    } else {
      return "";
    }
  }

  function GetPreviouPageString() { // 上一頁
    if (PagerBar.PreviouBarFormat != "") {
      if (PagerBar.RecordCount > PagerBar.PageSize && PagerBar.PageIndex != 1) {
        return PagerBar.PreviouBarFormat.replace(regexp, PagerBar.PageIndex - 1);
      } else {
        return "";
      }
    } else {
      return "";
    }
  }

  function GetNextPageString() {    // 下一頁
    if (PagerBar.NextBarFormat != "") {
      if (PagerBar.RecordCount > PagerBar.PageSize && PagerBar.PageIndex != PagerBar.PageCount) {
        return PagerBar.NextBarFormat.replace(regexp, PagerBar.PageIndex + 1);
      } else {
        return "";
      }
    } else {
      return "";
    }
  }

  function GetLastPageString() {    // 尾頁
    if (PagerBar.LastBarFormat != "" && PagerBar.PageIndex != PagerBar.PageCount
        && PagerBar.RecordCount != 0) {
      return PagerBar.LastBarFormat.replace(regexp, PagerBar.PageCount);
    } else {
      return "";
    }
  }

中間頁碼的計算稍微複雜,重點是讓當前頁盡量落在顯示範圍的中間,並在接近頭尾時自動貼邊:

  function GetPageString() {
    var pagestr = "";
    if (PagerBar.CurrentBarFormat != "" && PagerBar.PageBarFormat != "") {
      var ShowPageFirest = PagerBar.PageIndex - parseInt(PagerBar.ShowPagesCount / 2 + 1) < 0
        ? 0 : PagerBar.PageIndex - parseInt(PagerBar.ShowPagesCount / 2 + 1);

      if (PagerBar.PageCount < PagerBar.ShowPagesCount) {
        ShowPageFirest = 0;
      } else {
        if (PagerBar.PageIndex > (PagerBar.PageCount - parseInt(PagerBar.ShowPagesCount / 2 + 1))) {
          ShowPageFirest = PagerBar.PageCount - PagerBar.ShowPagesCount;
        }
      }

      for (var i = ShowPageFirest; i < ShowPageFirest + PagerBar.ShowPagesCount; i++) {
        if (PagerBar.PageIndex == i + 1) {
          pagestr += PagerBar.CurrentBarFormat.replace(regexp, i + 1);
        } else {
          pagestr += PagerBar.PageBarFormat.replace(regexp, i + 1);
        }
        if (i >= PagerBar.PageCount - 1) break;
      }
    }
    return pagestr;
  }

另有 GetFrontOtherPageString 與 GetAfterOtherPageString 負責前後跳段的「其他頁」提示,以及 GetCurrentRecordPageString 計算目前顯示的記錄區間(用 {0}、{1} 兩個佔位符):

  function GetCurrentRecordPageString() {
    if (PagerBar.CurrentRecordBarFormat != "") {
      if (PagerBar.RecordCount == 0) return "";
      return PagerBar.CurrentRecordBarFormat
        .replace("{0}", (PagerBar.PageIndex - 1) * PagerBar.PageSize + 1)
        .replace("{1}", PagerBar.PageIndex * PagerBar.PageSize > PagerBar.RecordCount
          ? PagerBar.RecordCount : PagerBar.PageIndex * PagerBar.PageSize);
    } else return "";
  }

三種輸出模式

對外只開三個方法,分別對應不同的完整度需求:

  // 全部模式:依各 IsShowXxx 開關決定要輸出哪些片段
  this.GetString = function (pageindex) {
    if (pageindex != null && NumberRegex.test(pageindex)) this.PageIndex = parseInt(pageindex);
    if (this.PageCount == 0) CalculatePageCount(this.PageSize, this.RecordCount);

    var pagestr = "";
    if (this.IsShowCurrentPageString)    pagestr += GetCurrentPageBarString();
    if (this.IsShowCurrentRecordString)  pagestr += GetCurrentRecordPageString();
    if (this.IsShowFristString)          pagestr += GetFristPageString();
    if (this.IsShowPreviouString)        pagestr += GetPreviouPageString();
    if (this.IsShowOtherString)          pagestr += GetFrontOtherPageString();
    if (this.IsShowPageString)           pagestr += GetPageString();
    if (this.IsShowOtherString)          pagestr += GetAfterOtherPageString();
    if (this.IsShowNextString)           pagestr += GetNextPageString();
    if (this.IsShowLastString)           pagestr += GetLastPageString();
    return pagestr;
  };

  // 標準模式:首頁 + 上一頁 + 頁碼 + 下一頁 + 尾頁
  this.GetNormalString = function (pageindex) {
    if (pageindex != null && NumberRegex.test(pageindex)) this.PageIndex = parseInt(pageindex);
    if (this.PageCount == 0) CalculatePageCount(this.PageSize, this.RecordCount);
    var pagestr = "";
    pagestr += GetFristPageString();
    pagestr += GetPreviouPageString();
    pagestr += GetPageString();
    pagestr += GetNextPageString();
    pagestr += GetLastPageString();
    return pagestr;
  };

  // 簡潔模式:只有上一頁 + 當前頁碼 + 下一頁
  this.GetSimpleString = function (pageindex) {
    if (pageindex != null && NumberRegex.test(pageindex)) this.PageIndex = parseInt(pageindex);
    if (this.PageCount == 0) CalculatePageCount(this.PageSize, this.RecordCount);
    var pagestr = "";
    pagestr += GetPreviouPageString();
    pagestr += GetCurrentPageBarString();
    pagestr += GetNextPageString();
    return pagestr;
  };
}

使用說明

目前還沒有附上使用範例,但用法輪廓很清楚:建立 PagerBar 實例並帶入總筆數與每頁筆數,設定各項 Format 屬性(頁碼外觀由你的格式字串決定),再呼叫 GetString() 取得 HTML 字串塞進容器。點擊時把頁索引傳回去,重新拉取該頁資料即可做到無刷新分頁。

要補充的是,這份程式屬於較早期的寫法。若在新專案中使用,可考慮改用 ES6 class 語法提升可讀性,並把 innerHTML 拼接改成建立 DOM 節點,避免格式字串中若有使用者輸入內容時產生 XSS 風險。

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