Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery datatables add class to tr

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');
        }
    }
});
like image 897
7 Reeds Avatar asked Sep 17 '15 21:09

7 Reeds


2 Answers

$('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");
like image 82
Ozan Avatar answered Oct 13 '22 18:10

Ozan


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');
    }
} );
like image 32
Zymawy Avatar answered Oct 13 '22 19:10

Zymawy