Deka87 Deka87 - 7 months ago 22
Javascript Question

Datatables: Select row with given ID

I've set up a datatables plugin and created a new table from an JSON file:

var table= $("#mytable").DataTable({

ajax: "list.json",
columns: [
{"data": "name"},
{"data": "location"},
{"data": "date"}
]
});


Now I want a add an
.active
class to a row with a given id:

table.on( 'xhr', function () {
table.row("#id_1").addClass("active");
}


(the id's for the rows has been defined during the plugin setup and are in place). However, I get this error:

undefined is not a function


like it can't find a row with this ID, however I do have it. Any ideas?

Answer

The Datatables .row() method doesn't return a DOM Node, you need to get it with .node() after selecting it.

var row = table.row("#id_1").node(); $(row).addClass('active');

Datatables .row()

Datatables .node()