jQuery数据表将类添加到tr

时间:2022-11-21 15:18:42

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和datatables。我想将一个类添加到特定行的TR元素。我知道如何找到这一行。 console.dir(行);显示行对象,以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');
        }
    }
});

3 个解决方案

#1


33  

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

$('tr',row)在行的上下文中查找tr元素,这意味着它将在作为context参数提供的行内搜索tr元素。

According to API, this should work

根据API,这应该工作

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

#2


1  

DataTable().row.add() situation:

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');

#3


1  

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');
    }
} );

`

`

#1


33  

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

$('tr',row)在行的上下文中查找tr元素,这意味着它将在作为context参数提供的行内搜索tr元素。

According to API, this should work

根据API,这应该工作

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

#2


1  

DataTable().row.add() situation:

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');

#3


1  

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');
    }
} );

`

`