Javascript jQuery 数据表向 tr 添加类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32640246/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery datatables add class to tr
提问by 7 Reeds
I am using jQuery and datatables. I want to add a class to the TR element of a particular row. I know how to find the row. The console.dir(row);
shows the row
object and that starts with a tr
element. I can't get the jQuery selector to do anything though. What am I missing?
我正在使用 jQuery 和数据表。我想向特定行的 TR 元素添加一个类。我知道如何找到该行。在console.dir(row);
示出了row
对象和与该启动tr
元件。我无法让 jQuery 选择器做任何事情。我错过了什么?
table = $('#resultTable').DataTable({
aaSorting: [],
ajax: {...},
columnDefs: [...],
createdRow: function (row, data, index) {
//
// if the second column cell is blank apply special formatting
//
if (data[1] == "") {
console.dir(row);
$('tr', row).addClass('label-warning');
}
}
});
回答by Ozan
回答by Hamza Zymawy
You would just have to use the createdRow
你只需要使用createdRow
$('#data-table').DataTable( {
createdRow: function( row, data, dataIndex ) {
// Set the data-status attribute, and add a class
$( row ).find('td:eq(0)')
.attr('data-status', data.status ? 'locked' : 'unlocked')
.addClass('asset-context box');
}
} );
回答by Nick Tsai
DataTable().row.add() situation:
DataTable().row.add() 情况:
If you want to add class when using row add function in Datatables, you could get the TR-DOM from node()
method:
如果要在 Datatables 中使用行添加功能时添加类,可以从node()
方法中获取 TR-DOM :
var datatable = $('#resultTable').DataTable();
var trDOM = datatable.row.add( [
"Col-1",
"Col-2"
] ).draw().node();
$( trDOM ).addClass('myClass');
回答by Majid Basirati
Also you can add class to tr
by pass through json data that you send to datatable. It's enough that every json item has DT_RowClass
.
For example:
您也可以tr
通过传递发送到数据表的 json 数据添加类。每个 json 项都有DT_RowClass
.
例如:
{
DT_RowAttr = new
{
attr1 = "1",
attr2 = "2"
}
DT_RowClass = "majid",
DT_RowId = "rowId"
}
In this example DT_RowId
value apply to id
attribute of any tr
tag and DT_RowAttr
value apply some custom attribute to tr
tag.
在此示例中,DT_RowId
值适用于id
任何tr
标签的属性,DT_RowAttr
值将一些自定义属性应用于tr
标签。
回答by Adel Mourad
To set a class name on the <tr>
use this calback
要在<tr>
使用此回调上设置类名
createdRow: function (row, data, dataIndex) {
$(row).addClass('some-class-name');
},
ref: https://datatables.net/reference/option/createdRow
参考:https: //datatables.net/reference/option/createdRow
To set a class on the <td>
use
设置一个类就<td>
使用
"columns": [
{
data:"",
className: "my_class",
render: function (data, type, row) { return "..."; }
},
{
data:"",
className: "my_class",
render: function (data, type, row) { return "..."; }
},
//...
]
Something similar for 'columnDefs'
“columnDefs”类似的东西
ref: https://datatables.net/reference/option/columns.className
参考:https: //datatables.net/reference/option/columns.className
To set the Id attribute on the row <tr>
use:
要在行上设置 Id 属性,请<tr>
使用:
//....
rowId: "ShipmentId",
columns: [...],
//....