DIV CSS 佈局教程網

用 JavaScript 替 JSON 資料做前端分頁的完整實作
編輯:JavaScript綜合知識     

後端一次回傳整批 JSON,前端要自己切頁顯示,是很常見的情境。本文示範如何在不動用任何框架的情況下,用原生 DOM 操作完成「上一頁/下一頁」的分頁表格。

資料準備

var a = {
  "code": 1,
  "list": [
    {"category1_id":"1","category1_name":"酒水饮料","category1_intro":"清涼可口的酒水饮料","category1_images":"https://www.songcuomen.com/attachments/productType/2014-01/10/21_1389343628.jpg"},
    {"category1_id":"2","category1_name":"米面积油","category1_intro":"家庭必備","category1_images":"https://www.songcuomen.com/attachments/productType/2014-01/10/21_1389343865.jpg"},
    {"category1_id":"3","category1_name":"美容洗护","category1_intro":"護膚必備","category1_images":"https://www.songcuomen.com/attachments/productType/2014-01/10/21_1389343935.jpg"},
    {"category1_id":"4","category1_name":"清潔洗浴","category1_intro":"給您一個乾淨的生活環境","category1_images":"https://www.songcuomen.com/attachments/productType/2014-01/10/21_1389343986.jpg"},
    {"category1_id":"5","category1_name":"生活用類品","category1_intro":"家庭必備","category1_images":"https://www.songcuomen.com/attachments/productType/2014-01/10/21_1389344445.jpg"},
    {"category1_id":"6","category1_name":"休閑食品","category1_intro":"好吃美味","category1_images":"https://www.songcuomen.com/attachments/productType/2014-01/10/21_1389344494.jpg"}
  ]
};

var y = 1;                        // 目前頁數
var t = 2;                        // 每頁顯示幾筆
var z = a["list"].length;         // 總筆數
var zy = Math.ceil(z / t);        // 總頁數

分頁函式

window.onload = function () {
  fanye(0);
};

function fanye(f) {
  if (y == 1 && f == -1) {
    alert('已經是第一頁了');
    f = 0;
  }
  if (y == zy && f == 1) {
    alert('已經是最後一頁了');
    f = 0;
  }

  var otbod = document.getElementById('table').tBodies[0];

  // 清空 tbody 現有的列
  var s = otbod.rows.length;
  for (i = 0; i < s; i++) {
    otbod.removeChild(otbod.rows[0]);
  }

  y += f;

  var q = (y - 1) * t;            // 本頁起始索引
  for (var i = 1; i < t + 1; i++) {
    var otr  = document.createElement('tr');
    var otd1 = document.createElement('td');
    var otd2 = document.createElement('td');
    var otd3 = document.createElement('td');
    var otd4 = document.createElement('td');

    otd1.innerHTML = a["list"][q]['category1_id'];
    otd2.innerHTML = a["list"][q]['category1_name'];
    otd3.innerHTML = a["list"][q]['category1_intro'];
    otd4.innerHTML = a["list"][q]['category1_images'];

    otr.appendChild(otd1);
    otr.appendChild(otd2);
    otr.appendChild(otd3);
    otr.appendChild(otd4);
    otbod.appendChild(otr);

    if (q == zy) { break; }
    q++;
  }
}

表格與翻頁連結

<table border="1" id="table">
  <thead>
    <tr>
      <td>category1_id</td>
      <td>category1_name</td>
      <td>category1_intro</td>
      <td>category1_images</td>
    </tr>
  </thead>
  <tbody></tbody>
</table>

<a href="javascript:fanye(1);">下一頁</a>
<a href="javascript:fanye(-1);">上一頁</a>

幾點可以改進的地方

這份程式把「每頁筆數」寫死成 2,實務上應該改成變數 t;邊界判斷用 alert 提示略顯粗糙,可以改成把連結 disable 掉。另外用 innerHTML 填入資料時,若內容來自外部輸入,務必先做轉義以避免 XSS。若日後資料量變大,更合理的做法是把分頁條件送到後端,只取當前頁的資料。

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