JQuery Datatables Columns API 参数详细说明

Data Tables: http://datatables.net/javascript

Version: 1.10.0css

 

Columns说明html

虽然咱们能够经过DOM直接获取DataTables元素的信息,可是DataTables提供了更方便的方法,能够自定义列的属性。下边就让咱们一块儿来学习DataTables是怎么来定义列属性的。java

  • DataTables提供了两个参数来定义列属性:columns 和 columnDefs (源代码里:aoColumns 和 aoColumnDefs)
  • 为了用户定义的参数更易于理解,DataTables提供的用户参数名和源代码的参数名是不同的,不过这两个参数名,无论使用哪一个,最终效果是同样的。(*如下参数说明都是用户使用参数名)

columns 和 columnDefs的区别:数组

  • 相同点:达到相同的效果
  • 不一样点:做用不同,使用不同(须要一个目标属性在每一个定义的对象(columnDefs.targetsDT))
  • columns:设置特定列的初始化属性,能够定义数组设置多列,数组长度必须等于表格的数量,只想使用默认值能够设为“NULL”,数组每一个元素只能设置单列的属性。
  • columnDefs:与columns很是类似,该数组能够针对特定的列,多列或者全部列定义。数组能够任意长度。经过targets参数设置一个列或者多列,该属性定义能够以下:
    • 0或正整数 - 从左边的列索引计数 
    • 负整数 - 列索引从右边计数
    • 一个字符串 - 类名称将被匹配上的TH为列 
    • 字符串“_all” - 全部的列(即指定一个默认值)
  • 两个参数能够同时使用,可是columns定义的优先级最高。
  • 当columnDefs里对同一列有多个定义时,最开始的定义优先级最高。

example:Js代码 app

 收藏代码

  1. $('#example').dataTable(  
  2.     {  
  3.         data: [  
  4.                 {  
  5.                     "name":    "Tiger Nixon1",  
  6.                     "position":   "System Architect1",  
  7.                     "phone": { "plain": 5552368, "filter": "5552368 555-2368", "display": "555-2368" },  
  8.                     "salary":    "$3,1201",  
  9.                     "start_date": "2011/04/25",  
  10.                     "office":    "Edinburgh1",  
  11.                     "extn":    "54211"  
  12.                 },  
  13.                 {  
  14.                     "name":    "Tiger Nixon2",  
  15.                     "position":   "System Architect2",  
  16.                     "phone": { "plain": 5552368, "filter": "5552368 555-2368", "display": "555-2368" },  
  17.                     "salary":    "$3,1202",  
  18.                     "start_date": "2011/04/25",  
  19.                     "office":    "Edinburgh2",  
  20.                     "extn":    "54212"  
  21.                 },  
  22.                 {  
  23.                     "name":    "Tiger Nixon3",  
  24.                     "position":   "System Architect3",  
  25.                     "phone": { "plain": 5552368, "filter": "5552368 555-2368", "display": "555-2368" },  
  26.                     "salary":    "$3,1203",  
  27.                     "start_date": "2011/04/25",  
  28.                     "office":    "Edinburgh3",  
  29.                     "extn":    "54213"  
  30.                 }  
  31.                   
  32.         ],  
  33.           
  34.         columnDefs: [  
  35.             {  
  36.                 "targets": 0,  
  37.                 "searchable": false  
  38.             },  
  39.             {  
  40.                 "targets": [1,2,3],  
  41.                 "orderData": [ 2, 3, 4 ],  
  42.                 "searchable": false  
  43.             },  
  44.             {  
  45.                 "targets": [-3,-4],  
  46.                 "orderable": false,  
  47.                 "searchable": false  
  48.             }  
  49.         ],  
  50.           
  51.         columns: [  
  52.             { "name": "name",   
  53.               "cellType": "th",  
  54.               "orderDataType": "dom-text",  
  55.               "orderSequence": [ "desc","asc", "asc" ],  
  56.               "className": "my_class",  
  57.               "contentPadding": "mmm",  
  58.               "createdCell": function (td, cellData, rowData, row, col) {  
  59.                   if ( row < 1 ) {  
  60.                     $(td).css('color', 'red');  
  61.                   }  
  62.                 },  
  63.               "data": "name",   
  64.               "searchable": true,   
  65.               "title": "My Name"  
  66.             },  
  67.             {   
  68.                 "data": "position",  
  69.                 "render": function ( data, type, full, meta ) {  
  70.                     return '<a href="'+data+'">' + data + '</a>';  
  71.                 }  
  72.             },  
  73.             {  
  74.                 "data": 'phone',  
  75.                 "render": {  
  76.                   "_": "plain",  
  77.                   "filter": "filter",  
  78.                   "display": "display"  
  79.                 }  
  80.             },  
  81.             { "data": "office" },  
  82.             { "data": "start_date", "type": "date" },  
  83.             { "data": "extn", "visible": false},  
  84.             { "data": "salary", "width": "20px"  },  
  85.             {  
  86.                 "data": null,  
  87.                 "orderable": false,  
  88.                 "defaultContent": "<button>Edit</button>"  
  89.             }  
  90.   
  91.         ]  
  92.     }  
  93. );  

  

参数详解:dom

用户参数名 源码参数名 英文解释 中文解释

cellType函数

 

sCellType学习

Cell type to be created for a column  设置列标签的类型(ex:th,td)
className

sClass优化

Class to assign to each cell in the column 设置列的class属性值
contentPadding

sContentPadding

Add padding to the text content used when calculating the optimal with for a table. 设置填充内容,以计算与优化为一个表时所使用的文本内容,通常不须要设置
createdCell

fnCreatedCell

Cell created callback to allow DOM manipulation 设置cell建立完后的回调函数,设置背景色或者添加行 
data

mData

Set the data source for the column from the rows data object / array 设置单元格里的值
defaultContent

sDefaultContent

Set default, static, content for a column 设置列的默认值
name

sName

Set a descriptive name for a column 设置列的描述性名称
orderable

bSortable

Enable or disable ordering on this column 设置列是否能够排序
orderData

aDataSort

Define multiple column ordering as the default order for a column 设置多列排序时列的默认顺序
orderDataType sSortDataType  Live DOM sorting type assignment  
orderSequence

asSorting

Order direction application sequence 设置列的默认排序,能够改变列排序的顺序处理
render

mRender

Render (process) the data for use in the table  
searchable

bSearchable

Enable or disable filtering on the data in this column 设置列的数据是否过滤
title sTitle  Set the column title 设置列的标题
type sType

Set the column type - used for filtering and sorting string processing.

Four types (string, numeric, date and html (which will strip HTML tags before ordering)) are currently available.

设置列的类型,用于过滤和排序的字符串处理。
visible bVisible Enable or disable the display of this column 设置列是否显示
width sWidth Column width assignment 定义列的宽度

 

参考资料:http://datatables.net/reference/option/

相关文章
相关标签/搜索