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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 14:01:21  来源:igfitidea点击:

jQuery datatables add class to tr

javascriptjquerydatatables

提问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 rowobject and that starts with a trelement. 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

$('tr', row)is looking for a tr element in the context of row, meaning it will search for a tr element insidethe rowprovided as context parameter.

$('tr', row)正在寻找行的背景下tr元素,这意味着它会为tr元素搜索里面row设置作为上下文参数。

According to API, this should work

根据API,这应该有效

$(row).addClass("label-warning");

回答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 trby 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_RowIdvalue apply to idattribute of any trtag and DT_RowAttrvalue apply some custom attribute to trtag.

在此示例中,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: [...],
//....