jQuery DataTables options 記錄

來源:互聯網
上載者:User

標籤:rtc   log   asc   info   而不是   object c   else   rip   efault   

參考文檔:DataTables reference page

簡介

columns 是一個數組,用於指定DataTables包含的列的定義,每項包含的屬性包括:

columns.data: 用於指定欄位名,可能的值包括:

  • undefined: 當指定為undefined時,將會使用columns.defaultContent代替; 如果沒有指定columns.defaultContent, dataTables 會報錯;
  • null: 當指定為null時,如果columns.render有被定義,傳遞給rendering function的資料將是未經處理資料源的當前行的所有資料;如果沒有renderer, columns.defaultContent 將會起作用;如果沒有指定defaultContent,顯示資料將會是Null 字元串。null 可用於所有其他的資料類型。
  • function: 如果指定為一個函數時,函數會被執行,傳回值將作為顯示內容。在DataTables 1.10.1中,這個函數會在和當前行的資料對象相同的範圍內執行。

data屬性是一個可以設定和讀取的選項。如果你只是需要格式化資料輸出,columns.render 選項會更好,因為它是唯讀,並且使用起來更加簡單。

DataTables 1.10.3 以後,data 選項可以和一個DOM 資料來源一起使用,指示DataTables為DOM資料來源對象中的每列的資料應該存到哪裡。預設情況下,DataTables 將把這些資料存在一個數組裡。但是使用這一選項,你可以提供對象屬性名稱來描述使用的對象的結構。

data屬性可以使用的類型integer:

指定資料來源中的數組索引。這是DataTables預設使用的值(每列遞增)。

string:

讀和寫的對象時資料來源的一個對象屬性。有3種特殊的選項值可用來改變DataTables從資料來源對象中讀取資料的方式:

. – Dotted Javascript notation. 就像你在Javascript中使用 . 讀取嵌套對象一樣,這也可以在data屬性中使用。例如:browser.version 或是 browser.name。 如果對象參數名中包含 .,使用\\ 轉義,如first\\.name。

[] – Array notation. DataTables 能夠自動合并數組資料來源的資料,使用提供的字元合并在中括弧之間的資料。比如:name[, ] 將產生一個來自原數組的,以逗號分隔的序列。如果中括弧之間沒有指定字元, 將會返回原始數組的資料。

() – Function notation. 在參數結尾添加() 將會執行給定名稱的函數。如:browser() 是在資料來源上執行的一個簡單函數;browser.version() 是一個嵌套屬性上的函數,甚至 browser().version 是從函數傳回值中擷取一個對象屬性。推薦在render 中使用function notation而不是在data中,因為它作為renderer使用起來更簡單。

 

null:

直接使用當前行的未經處理資料源, 而不是中未經處理資料源中摘取。這一動作會對其他兩個初始化選項有影響:

columns.defaultContent : 當data選項值為null 並且當前列的defaultContent有指定時,目前的儲存格將使用defaultContent中定義的值。

columns.render: 當data選項值為null 並且render 選項有指定時,當前行的整個資料來源將被renderer使用。

 

object:

對DataTables請求的不同資料類型使用不同的資料(filter, display, type, sort)。 對象的屬性名稱是屬性執行的資料類型,並且屬性值可以使用integer, string 或是函數來定義,columns.data的規則同樣適用。注意 必須指定 _ 選項。這是預設值,如果你沒有指定DataTables 請求的資料類型。如:

"data":{    "_": "phone",    "filter": "phone_filter",    "display": "phone_display"}

 

function data(row, type, set, meta)

當DataTables 需要讀取或是這種儲存格的值的時候,給定的函數將被執行。

該函數可能會被調用多次,因為DataTables 會為它需要的不同的資料類型調用該函數 – sorting, filtering 和 display.

參數定義:

  Name Type Optional Description
1 row array,object No The data for the whole row
2 type string No The data type requested for the cell
3 set Any No Value to set if the type parameter is set. Otherwise, undefined.
4 meta object No Since 1.10.1: An object that contains additional information about the cell being requested. This object contains the following properties:
row – The row index for the requested cell.
col – The column index for the requested cell.
settings – The DataTables.Settings object for the table in question. This can be used to obtain an API instance if required.

當調用類型是set的時候,函數傳回值不是必須的,但是其他情況下傳回值被用於請求的資料。

 

Default

自動使用列的索引值。

樣本從對象中讀取表資料:
// JSON structure for each row in this example://   {//      "engine": {value},//      "browser": {value},//      "platform": {value},//      "version": {value},//      "grade": {value}//   }$(‘#example‘).dataTable( {  "ajaxSource": "sources/objects.txt",  "columns": [    { "data": "engine" },    { "data": "browser" },    { "data": "platform" },    { "data": "version" },    { "data": "grade" }  ]} );

 

從深度嵌套對象中讀取資訊:
// JSON structure for each row://   {//      "engine": {value},//      "browser": {value},//      "platform": {//         "inner": {value}//      },//      "details": [//         {value}, {value}//      ]//   }$(‘#example‘).dataTable( {  "ajaxSource": "sources/deep.txt",  "columns": [    { "data": "engine" },    { "data": "browser" },    { "data": "platform.inner" },    { "data": "platform.details.0" },    { "data": "platform.details.1" }  ]} );
把DOM 資料表讀取到資料對象中:
$(document).ready(function() {    $(‘#example‘).DataTable({        "columns": [            { "data": "name" },            { "data": "position" },            { "data": "office" },            { "data": "age" },            { "data": "start_date" },            { "data": "salary" }        ]    });} );

 

使用函數data 對sorting, filtering 和displaying 提供不同的資訊:
$(‘#example‘).dataTable( {  "columnDefs": [ {    "targets": 0,    "data": function ( row, type, val, meta ) {      if (type === ‘set‘) {        row.price = val;        // Store the computed display and filter values for efficiency        row.price_display = val=="" ? "" : "$"+numberFormat(val);        row.price_filter  = val=="" ? "" : "$"+numberFormat(val)+" "+val;        return;      }      else if (type === ‘display‘) {        return row.price_display;      }      else if (type === ‘filter‘) {        return row.price_filter;      }      // ‘sort‘, ‘type‘ and undefined all just use the integer      return row.price;    }  } ]} );

 

使用預設內容:
$(‘#example‘).dataTable( {  "columnDefs": [ {    "targets": [ 0 ],    "data": null,    "defaultContent": "Click to edit"  } ]} );

 

使用array notation – 從數組中輸出一個序列:
$(‘#example‘).dataTable( {  "columnDefs": [ {    "targets": [ 0 ],    "data": "name[, ]"  } ]} );

 

該文翻譯DataTables 官方文檔,僅作記錄複習只用。

jQuery DataTables options 記錄

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.