DIV CSS 佈局教程網

jQuery 分頁 Page 物件使用說明與完整範例
編輯:JQuery特效代碼     

這是一個不依賴外掛、純 jQuery 手寫的分頁物件,支援 URL 跳轉與回呼函式兩種模式,也能輸出「1 … 4 5 6 … 20」這種摺疊式頁碼。

參數說明

  • displayId:頁碼顯示區域的 id,預設 pageBox,可省略
  • pagesize:每頁筆數,預設 15,可省略
  • totalsize:總筆數
  • curpage:目前頁碼
  • simple:預設 false;設為 true 時不顯示上一頁/下一頁
  • type:0 為預設的 HTTP 跳轉;1 為 callback 模式,頁面必須實作 pageCallBack(pageNum),頁碼從 1 開始
  • url:連結位址,type 為 1 時可省略

呼叫範例:

Page._run({totalsize:300, curpage:11, type:1, simple:true});

完整程式碼

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Page 物件</title>
<style>
.pageBox,.pageBox1{ text-align:center; height:25px; padding:15px 0;}
.pageBox .pages a.up,.pageBox .pages a.down{ color:#6eb4ea; text-decoration:none; border:1px solid #ffffff; background:none;}
.pageBox .pages a.else{ background:none; border:none;}
.pageBox .pages a{ padding:3px 7px; border:1px solid #f3f6f6; background:#fdfdfd; margin:0 5px; color:#999999;}
.pageBox .pages a:hover,.pageBox .pages a.hover{ background:#6eb4ea; border:1px solid #6eb4ea; color:#ffffff; text-decoration:none;padding:2px 7px; }
.pageBox .pages span{ padding:3px 7px; color:#999999;}
.fiv_sep{ height:3px; float:left; width:100%; font-size:4px; line-height:2px;}
</style>
<script type="text/javascript" src="../jquery/jquery.js"></script><!-- 需 jQuery 1.4 以上版本 -->
</head>
<body>
<h1>Page</h1>
<div class="pageBox" id="pageBox"></div>
<script type="text/javascript">
var Page = {
  /**
   * displayId 顯示區域 id,預設 pageBox
   * pagesize  每頁筆數,預設 15
   * totalsize 總筆數
   * curpage   目前頁數
   * simple    預設 false,true 時不顯示上一頁與下一頁
   * type      0 走 http 跳轉,1 傳入回呼函數
   * url       連結位址,type 為 1 時可省略
   */
  _run: function (param) {
    var totalpages = 1,          // 總頁數
        displayId = "#pageBox",  // 顯示區域 id
        pagesize = 15,           // 每頁筆數
        totalsize = 0,           // 總筆數
        curpage = 1,             // 目前頁數
        url = "",                // 連結位址
        type = 0,                // 0 走 http 跳轉,1 傳入回呼函數
        simple = false;          // 簡易版,無上一頁與下一頁

    if (param.type != undefined) type = param.type;
    if (param.displayId != undefined) displayId = param.displayId;
    if (param.pagesize != undefined) pagesize = param.pagesize;
    if (param.totalsize != undefined) totalsize = param.totalsize;
    if (param.curpage != undefined) curpage = param.curpage;
    if (param.url != undefined) url = param.url;
    if (param.simple != undefined) simple = param.simple;

    if (url.indexOf("?") == -1) {
      url += "?1=1";
    }

    if (totalsize > 0) {
      totalpages = Page._getTotalPages(totalsize, pagesize);
      if (curpage > totalpages) { curpage = totalpages; } // 傳入頁數大於總頁數時,以最後一頁計

      if (totalpages > 1) {
        var firstPage = simple ? "" : Page._builderPageArea(type, "up", url, curpage - 1, "上一頁", false, displayId),
            lastPage  = simple ? "" : Page._builderPageArea(type, "down", url, parseInt(curpage) + 1, "下一頁", false, displayId),
            pages = new Array();

        if (curpage <= 4) {           // 接近第一頁,無上一頁
          if (curpage != 1) { pages.push(firstPage); }
          if (totalpages > 5) {
            for (var i = 1; i <= 5; i++) {
              pages.push(Page._builderPageArea(type, "", url, i, i, curpage == i, displayId));
            }
            pages.push('<span>...</span>');
            pages.push(Page._builderPageArea(type, "", url, totalpages, totalpages, false, displayId));
          } else {
            for (var i = 1; i <= totalpages; i++) {
              pages.push(Page._builderPageArea(type, "", url, i, i, curpage == i, displayId));
            }
          }
          if (curpage != totalpages) pages.push(lastPage);

        } else if (totalpages - curpage <= 4) {  // 接近最後一頁,無下一頁
          if (curpage != 1) { pages.push(firstPage); }
          if (totalpages > 5) {
            pages.push(Page._builderPageArea(type, "", url, 1, 1, false, displayId));
            pages.push('<span>...</span>');
            for (var i = totalpages - 4; i <= totalpages; i++) {
              pages.push(Page._builderPageArea(type, "", url, i, i, curpage == i, displayId));
            }
          } else {
            for (var i = 1; i <= totalpages; i++) {
              pages.push(Page._builderPageArea(type, "", url, i, i, curpage == i, displayId));
            }
          }
          if (curpage != totalpages) pages.push(lastPage);

        } else {                      // 中間頁:顯示前後各兩頁並摺疊頭尾
          pages.push(firstPage);
          pages.push(Page._builderPageArea(type, "", url, 1, 1, false, displayId));
          pages.push('<span>...</span>');
          for (var i = curpage - 2; i <= curpage + 2; i++) {
            pages.push(Page._builderPageArea(type, "", url, i, i, curpage == i, displayId));
          }
          pages.push('<span>...</span>');
          pages.push(Page._builderPageArea(type, "", url, totalpages, totalpages, false, displayId));
          pages.push(lastPage);
        }

        var result = new Array();
        result.push('<div class="pages">');
        result.push(pages.join(''));
        result.push('</div>');
        $(displayId).html(result.join(''));
      }
    }
  },

  /** 計算總頁數 */
  _getTotalPages: function (_totalsize, _pagesize) {
    if (_totalsize % _pagesize == 0)
      return _totalsize / _pagesize;
    else
      return parseInt(_totalsize / _pagesize) + 1;
  },

  /** 產生單一頁碼區塊 */
  _builderPageArea: function (type, textType, url, page, text, _focus, _displayId) {
    var hrefStr, href = new Array();
    if (type == 0) {
      href.push(url);
      href.push('&pagenum=');
      href.push(page);
    } else if (type == 1) {
      href.push('javascript:void(0);pageCallBack(\'');
      href.push(page);
      href.push('\',\'');
      href.push(_displayId);
      href.push('\')');
    }
    hrefStr = href.join('');

    var result = new Array();
    if (textType == 'up') {
      result.push('<a href="');
      result.push(hrefStr);
      result.push('" class="up">上一頁</a>');
    } else if (textType == 'down') {
      result.push('<a href="');
      result.push(hrefStr);
      result.push('" class="down">下一頁</a>');
    } else {
      result.push('<a href="');
      result.push(hrefStr);
      if (_focus) {
        result.push('" style="background:#6EB4EA;color:#FFF;">');
      } else {
        result.push('">');
      }
      result.push(page);
      result.push('</a>');
    }
    return result.join('');
  }
};
Page._run({totalsize:100, curpage:1, pagesize:10});
</script>
</body>
</html>

使用要點

走 callback 模式時,頁面必須自行實作 pageCallBack,例如:

function pageCallBack(pageNum, displayId){
  // 依 pageNum 重新取資料並更新畫面
  loadData(pageNum);
}

另外,_run 每次呼叫都會重算並覆寫顯示區,因此 AJAX 更新資料後重新呼叫一次即可,不必自己拼接 HTML。

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