relo80
relo80

Reputation: 285

Adding a class by id to a datatables row

i want to add a class to a specific row (by id of the row).

I trie to do it in this way, but it failed.

var table = $('#mytableID').DataTable();            
var myrow = $(table.row('#my_row_id').node());
table.row(myrow).addClass('mynewclass');

What i do wrong?

Please note, that the datatables has pagination pages and my content-datas comes from a ajax call

Upvotes: 1

Views: 3086

Answers (1)

andrewJames
andrewJames

Reputation: 21911

You are close with your example code, but here is an updated version:

var table = $('#mytableID').DataTable();
table.row('#my_row_id').nodes().to$().addClass('mynewclass');

This is a bit tricky, because although you can use row() to select a single row (the ID is supposed to be unique, of course), you need to use nodes() (the plural form, and not node()) to select the node.

See also to$(), for reference.

This works across the entire table, regardless of pagination.

For example, if I have the following row of HTML table data:

<tr id="my_row_id">
    <td>Herrod Chandler</td>
    <td>Sales Assistant</td>
    <td>San Francisco</td>
    <td>59</td>
    <td>2012/08/06</td>
    <td>$137,500</td>
</tr>

Then my code will insert the required class, even though the row is initially on page 3 of the results.

enter image description here

Upvotes: 1

Related Questions