jQuery DataTables外掛程式從伺服器端擷取資料的方法

來源:互聯網
上載者:User

  sAjaxSource參數,值是url。table會發送ajax請求,從伺服器端擷取資料。伺服器端返回的資料應該是一個可以被轉換成JSON對象的JSON字串。這個字串必須嚴格符合JSON格式的要求。否則會出錯。該資料對象該對象的key應該是“aaData”,例如:

  Js代碼:

  {

  "aaData":

  {

  "columnA":"valueA",

  "columnB":"valueB",

  ...

  }

  }

  bServerSide參數,設定成true,表示使用伺服器端處理資料。當排序時,會直接到後台查詢資料,直接顯示,不會在前端進行排序操作。

  fnServerData參數,用來自訂函數,代替DataTables外掛程式預設的從伺服器端查詢資料的函數。預設的函數如下:

  原文參考自站長網:http://www.software8.co/wzjs/jquery/3314.html

  Js代碼:

  /**

  * @param {string} sSource HTTP source to obtain the data from (sAjaxSource)

  * @param {array} aoData A key/value pair object containing the data to send

  * to the server

  * @param {function} fnCallback to be called on completion of the data get

  * process that will draw the data on the page.

  * @param {object} oSettings DataTables settings object

  */

  "fnServerData": function ( sUrl, aoData, fnCallback, oSettings ) {

  oSettings.jqXHR = $.ajax( {

  "url": sUrl,

  "data": aoData,

  "success": function (json) {

  if ( json.sError ) {

  oSettings.oApi._fnLog( oSettings, 0, json.sError );

  }

  $(oSettings.oInstance).trigger('xhr', [oSettings, json]);

  fnCallback( json );

  },

  "dataType": "json",

  "cache": false,

  "type": oSettings.sServerMethod,

  "error": function (xhr, error, thrown) {

  if ( error == "parsererror" ) {

  oSettings.oApi._fnLog( oSettings, 0, "DataTables warning: JSON data from " + "server could not be parsed. This is caused by a JSON formatting error." );

  }

  }

  });

  },

  我們可以用這個參數來自訂ajax請求,也可以對擷取到的資料進行處理等操作。例如:

  伺服器端之返回表格的資料對象,沒有用“aaData”作為資料的key,我們就可以在我們定義的回呼函數裡面,給資料加上“aaData” key。

  fnServerParams參數,用來發送額外的資料給伺服器。例如:

  Js代碼:

  $('#example').dataTable( {

  "bProcessing": true,

  "bServerSide": true,

  "sAjaxSource": "scripts/server_processing.php",

  "fnServerParams": function ( aoData ) {

  aoData.push( { "name": "more_data", "value": "my_value" } );

  }

  });

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.