JQuery Datatables Columns API 参数详细说明

Data Tables: http://datatables.net/

Version: 1.10.0

Columns说明

虽然我们可以通过DOM直接获取DataTables元素的信息,但是DataTables提供了更方便的方法,可以自定义列的属性。下边就让我们一起来学习DataTables是怎么来定义列属性的。

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

columnscolumnDefs的区别:

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

example:

$('#example').dataTable(  
    {  
        data: [  
                {  
                    "name":    "Tiger Nixon1",  
                    "position":   "System Architect1",  
                    "phone": { "plain": 5552368, "filter": "5552368 555-2368", "display": "555-2368" },  
                    "salary":    "$3,1201",  
                    "start_date": "2011/04/25",  
                    "office":    "Edinburgh1",  
                    "extn":    "54211"  
                },  
                {  
                    "name":    "Tiger Nixon2",  
                    "position":   "System Architect2",  
                    "phone": { "plain": 5552368, "filter": "5552368 555-2368", "display": "555-2368" },  
                    "salary":    "$3,1202",  
                    "start_date": "2011/04/25",  
                    "office":    "Edinburgh2",  
                    "extn":    "54212"  
                },  
                {  
                    "name":    "Tiger Nixon3",  
                    "position":   "System Architect3",  
                    "phone": { "plain": 5552368, "filter": "5552368 555-2368", "display": "555-2368" },  
                    "salary":    "$3,1203",  
                    "start_date": "2011/04/25",  
                    "office":    "Edinburgh3",  
                    "extn":    "54213"  
                }  

        ],  

        columnDefs: [  
            {  
                "targets": 0,  
                "searchable": false  
            },  
            {  
                "targets": [1,2,3],  
                "orderData": [ 2, 3, 4 ],  
                "searchable": false  
            },  
            {  
                "targets": [-3,-4],  
                "orderable": false,  
                "searchable": false  
            }  
        ],  

        columns: [  
            { "name": "name",   
              "cellType": "th",  
              "orderDataType": "dom-text",  
              "orderSequence": [ "desc","asc", "asc" ],  
              "className": "my_class",  
              "contentPadding": "mmm",  
              "createdCell": function (td, cellData, rowData, row, col) {  
                  if ( row < 1 ) {  
                    $(td).css('color', 'red');  
                  }  
                },  
              "data": "name",   
              "searchable": true,   
              "title": "My Name"  
            },  
            {   
                "data": "position",  
                "render": function ( data, type, full, meta ) {  
                    return '<a href="'+data+'">' + data + '</a>';  
                }  
            },  
            {  
                "data": 'phone',  
                "render": {  
                  "_": "plain",  
                  "filter": "filter",  
                  "display": "display"  
                }  
            },  
            { "data": "office" },  
            { "data": "start_date", "type": "date" },  
            { "data": "extn", "visible": false},  
            { "data": "salary", "width": "20px"  },  
            {  
                "data": null,  
                "orderable": false,  
                "defaultContent": "<button>Edit</button>"  
            }  

        ]  
    }  
);  

参数详解:

用户参数名源码参数名英文解释中文解释
cellTypesCellTypeCell type to be created for a column设置列标签的类型(ex:th,td)
classNamesClassClass to assign to each cell in the column设置列的class属性值
contentPaddingsContentPaddingAdd padding to the text content used when calculating the optimal with for a table.设置填充内容,以计算与优化为一个表时所使用的文本内容,一般不需要设置
createdCellfnCreatedCellCell created callback to allow DOM manipulation设置cell创建完后的回调函数,设置背景色或者添加行
datamDataSet the data source for the column from the rows data object / array设置单元格里的值
defaultContentsDefaultContentSet default, static, content for a column设置列的默认值
namesNameSet a descriptive name for a column设置列的描述性名称
orderablebSortableEnable or disable ordering on this column设置列是否可以排序
orderDataaDataSortDefine multiple column ordering as the default order for a column设置多列排序时列的默认顺序
orderDataTypesSortDataTypeLive DOM sorting type assignment
orderSequenceasSortingOrder direction application sequence设置列的默认排序,可以改变列排序的顺序处理
rendermRenderRender (process) the data for use in the table
searchablebSearchableEnable or disable filtering on the data in this column设置列的数据是否过滤
titlesTitleSet the column title设置列的标题
typesTypeSet 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.设置列的类型,用于过滤和排序的字符串处理。
visiblebVisibleEnable or disable the display of this column设置列是否显示
widthsWidthColumn width assignment定义列的宽度

jQuery DataTables插件 aoColumnDefs和aoColumns的用法

aoColumnDefsaoColumns都可以设置列的属性。

aoColumnDefs设置列的属性时,可以任意指定列,并且不需要给所有列都设置。

aoColumns设置列时,不可以任意指定列,必须列出所有列。如果某一列不需要设置,则要赋值null

如果aoColumnDefsaoColumns同时给同一列的同一个属性设置了值,那么aoColumns的优先级要高。

另外,如果aoColumnDefs使用 target :["_all"] 给所有列设置了通用的属性,然后又为某一列的该属性单独设置了值,那么这两个设置先设置的优先级高,例如:

aoColumnDefs : [  
    {  
        "aTargets" : [1],  
        "mRender" : function(data, type, full){  
            return 100;  
        }  
    },  
     {  
        "aTargets" : ["_all"],  
        "mRender" : function(data, type, full){  
            return 200;  
        }  
    }  
]  

aTargets是1的设置的优先级高。

如果写成:

aoColumnDefs : [  
    {  
        "aTargets" : ["_all"],  
        "mRender" : function(data, type, full){  
            return 200;  
        }  
    },  
    {  
        "aTargets" : [1],  
        "mRender" : function(data, type, full){  
            return 100;  
        }  
    }  
]  

aTargets是”_all“的优先级高

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值