61

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?

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');
        }
    }
});
7 Reeds
  • 2,419
  • 3
  • 32
  • 64
  • 2
    Can't you just do `$(row).addClass('label-warning');`? – Marc Sep 17 '15 at 21:34
  • 2
    Why aren't you doing `$(row).addClass(...)`? If `row` is a `tr` itself, `$('tr', row)` (or `$(row).find('tr')`) will only find nested `tr`s. – Kenney Sep 17 '15 at 21:35
  • 1
    heh, I *thought* I tried `$(row).addClass("label-warning");` without success... now it works. :-} – 7 Reeds Sep 17 '15 at 21:45

7 Answers7

69

$('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.

According to API, this should work

$(row).addClass("label-warning");
Ozan
  • 3,709
  • 2
  • 18
  • 23
  • 1
    Thanks, as above I *thought* I had done this but I must have been making some other error. Thanks – 7 Reeds Sep 17 '15 at 21:46
37

You would just have to use the 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');
    }
} );
Roko C. Buljan
  • 196,159
  • 39
  • 305
  • 313
Zymawy
  • 1,511
  • 16
  • 15
  • 1
    If you don't want the first TD but just want to add a class to your tr use this line: $(row).addClass('asset-context box'); – TroySteven Jun 29 '20 at 21:04
13

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:

var datatable = $('#resultTable').DataTable();

var trDOM = datatable.row.add( [
    "Col-1",
    "Col-2"
] ).draw().node();

$( trDOM ).addClass('myClass');
Nick Tsai
  • 3,799
  • 33
  • 36
12

To set the Id attribute on the row <tr> use:

//.... 
rowId: "ShipmentId",
columns: [...],
//....

To set a class name on the <tr> use this calback

createdRow: function (row, data, dataIndex) {
    $(row).addClass('some-class-name');
},

ref: https://datatables.net/reference/option/createdRow

To set a class on the <td> use

"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'

ref: https://datatables.net/reference/option/columns.className

Adel Mourad
  • 1,351
  • 16
  • 13
  • This adds it to the `` not the `` – ricks Apr 27 '20 at 19:25
  • is any way to change the classname based on data into render function? so first define a default Classname and then in render function check if data is not valid change the classname – vsam Dec 07 '22 at 09:51
  • @vsam in this case you may wrap and cell contents inside a div or so and set the class name on it rather on the cell - the td - itself – Adel Mourad Dec 09 '22 at 08:58
5

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:

{

    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.

Majid Basirati
  • 2,665
  • 3
  • 24
  • 46
0

Another solution:

createdRow: function (row,data) {
    var stsId = data.Ise_Sts_Cost_ID;
    if (stsId == 3)
        $(row).addClass('table-warning');
    else if (stsId == 4)
        $(row).addClass('table-success');
    else
        $(row).addClass('table-danger');
}
Majid Basirati
  • 2,665
  • 3
  • 24
  • 46
0

You can also use the stripeClasses option, which is used to set the row classname for the odd and even stripes.

stripeClasses: ['odd align-middle', 'even align-middle'],

In this example, I have attached the bootstrap 'align-middle' class to the original stripe classes, to vertically align the text within each row.

Ref: https://datatables.net/reference/option/stripeClasses