jQuery DataTables插件自定義Ajax分頁實(shí)例解析

字號:


    這篇文章主要為大家詳細(xì)介紹了jQuery DataTables插件自定義Ajax分頁的相關(guān)資料,需要的朋友可以參考下
    一、問題描述
    園友是做前端的,產(chǎn)品經(jīng)理要求他使用jQuery DataTables插件顯示一個(gè)列表,要實(shí)現(xiàn)分類效果。
    后端的分頁接口已經(jīng)寫好了,不涉及條件查詢,需要傳入頁碼(pageNo)和頁面顯示數(shù)據(jù)條數(shù)(pageSize),顯示相應(yīng)頁的顯示記錄,且不能修改后端接口。
    二、分析
    先來分析下分頁實(shí)現(xiàn)。
    一是后端分頁:這種情況下,在后端很容易實(shí)現(xiàn),在官網(wǎng)上有示例,不多說明。
    二是前端分頁:前端分頁也是支持的,不過需要一次把所有數(shù)據(jù)都獲取到才可以。
    看到這里,問題來了。由于后端在目前的情況下是更改不了,只能在前端實(shí)現(xiàn)。但是,現(xiàn)在又不滿足前端分頁的條件:
    一次性獲取所有數(shù)據(jù)(現(xiàn)在后端數(shù)據(jù)接口只能返回相應(yīng)頁碼的數(shù)據(jù))。
    介于目前的情況,獲取的數(shù)據(jù)只有一頁,沒有所有的頁碼。
    試試能不能偽裝一下后端分頁的情況,就是開啟后端分頁,在請求之前,將傳入的數(shù)據(jù)進(jìn)行重組,在獲取到數(shù)據(jù)后,將返回的數(shù)據(jù)按照后端分頁的數(shù)據(jù)格式組裝一遍。
    經(jīng)過測試,是可以的。
    三、實(shí)現(xiàn)
    通過DataTables配置參數(shù)ajax項(xiàng)實(shí)現(xiàn)的。關(guān)于ajax詳細(xì)介紹請看官方說明:中文 | 英文
    ajax接收三種類型的參數(shù):
    *string: 設(shè)置獲取數(shù)據(jù)的url
    *object:和 jQuery.ajax 定義類似
    *function:自定義獲取數(shù)據(jù)的功能
    直接上代碼吧,都有注釋。
    前端頁面代碼:
    代碼如下:
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
     <title>jquery DataTables插件自定義分頁ajax實(shí)現(xiàn)</title>
     <link rel="stylesheet" media="screen">
     <link rel="stylesheet" media="screen">
     <link rel="stylesheet" media="screen">
    </head>
    <body>
    <div>
     <h3>JQuery DataTables插件自定義分頁Ajax實(shí)現(xiàn)</h3>
     <table id="example" cellspacing="0">
     <thead>
     <tr>
      <th>編號</th>
      <th>姓名</th>
      <th>性別</th>
     </tr>
     </thead>
     </table>
    </div>
    <script src="http://cdn.bootcss.com/datatables/1.10.11/js/jquery.js"></script>
    <script src="http://cdn.bootcss.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
    <script src="http://cdn.bootcss.com/datatables/1.10.11/js/jquery.dataTables.min.js"></script>
    <script src="http://cdn.bootcss.com/datatables/1.10.11/js/dataTables.bootstrap.min.js"></script>
    <script type="text/javascript">
     $(function () {
     //提示信息
     var lang = {
      "sProcessing": "處理中...",
      "sLengthMenu": "每頁 _MENU_ 項(xiàng)",
      "sZeroRecords": "沒有匹配結(jié)果",
      "sInfo": "當(dāng)前顯示第 _START_ 至 _END_ 項(xiàng),共 _TOTAL_ 項(xiàng)。",
      "sInfoEmpty": "當(dāng)前顯示第 0 至 0 項(xiàng),共 0 項(xiàng)",
      "sInfoFiltered": "(由 _MAX_ 項(xiàng)結(jié)果過濾)",
      "sInfoPostFix": "",
      "sSearch": "搜索:",
      "sUrl": "",
      "sEmptyTable": "表中數(shù)據(jù)為空",
      "sLoadingRecords": "載入中...",
      "sInfoThousands": ",",
      "oPaginate": {
      "sFirst": "首頁",
      "sPrevious": "上頁",
      "sNext": "下頁",
      "sLast": "末頁",
      "sJump": "跳轉(zhuǎn)"
      },
      "oAria": {
      "sSortAscending": ": 以升序排列此列",
      "sSortDescending": ": 以降序排列此列"
      }
     };
     //初始化表格
     var table = $("#example").dataTable({
      language:lang, //提示信息
      autoWidth: false, //禁用自動(dòng)調(diào)整列寬
      stripeClasses: ["odd", "even"], //為奇偶行加上樣式,兼容不支持CSS偽類的場合
      processing: true, //隱藏加載提示,自行處理
      serverSide: true, //啟用服務(wù)器端分頁
      searching: false, //禁用原生搜索
      orderMulti: false, //啟用多列排序
      order: [], //取消默認(rèn)排序查詢,否則復(fù)選框一列會(huì)出現(xiàn)小箭頭
      renderer: "bootstrap", //渲染樣式:Bootstrap和jquery-ui
      pagingType: "simple_numbers", //分頁樣式:simple,simple_numbers,full,full_numbers
      columnDefs: [{
      "targets": 'nosort', //列的樣式名
      "orderable": false //包含上樣式名‘nosort'的禁止排序
      }],
      ajax: function (data, callback, settings) {
      //封裝請求參數(shù)
      var param = {};
      param.limit = data.length;//頁面顯示記錄條數(shù),在頁面顯示每頁顯示多少項(xiàng)的時(shí)候
      param.start = data.start;//開始的記錄序號
      param.page = (data.start / data.length)+1;//當(dāng)前頁碼
      //console.log(param);
      //ajax請求數(shù)據(jù)
      $.ajax({
       type: "GET",
       url: "/hello/list",
       cache: false, //禁用緩存
       data: param, //傳入組裝的參數(shù)
       dataType: "json",
       success: function (result) {
       //console.log(result);
       //setTimeout僅為測試延遲效果
       setTimeout(function () {
        //封裝返回?cái)?shù)據(jù)
        var returnData = {};
        returnData.draw = data.draw;//這里直接自行返回了draw計(jì)數(shù)器,應(yīng)該由后臺返回
        returnData.recordsTotal = result.total;//返回?cái)?shù)據(jù)全部記錄
        returnData.recordsFiltered = result.total;//后臺不實(shí)現(xiàn)過濾功能,每次查詢均視作全部結(jié)果
        returnData.data = result.data;//返回的數(shù)據(jù)列表
        //console.log(returnData);
        //調(diào)用DataTables提供的callback方法,代表數(shù)據(jù)已封裝完成并傳回DataTables進(jìn)行渲染
        //此時(shí)的數(shù)據(jù)需確保正確無誤,異常判斷應(yīng)在執(zhí)行此回調(diào)前自行處理完畢
        callback(returnData);
       }, 200);
       }
      });
      },
      //列表表頭字段
      columns: [
      { "data": "Id" },
      { "data": "Name" },
      { "data": "Sex" }
      ]
     }).api();
     //此處需調(diào)用api()方法,否則返回的是JQuery對象而不是DataTables的API對象
     });
    </script>
    </body>
    </html>
    JSON數(shù)據(jù)格式:
    名單
    效果圖:
    名單
    本文就介紹到這里,希望對大家的學(xué)習(xí)有所幫助。