The best way to select a row at table initialization stage
The best way to select a row at table initialization stage

Hello,
I have a DataTable which first column is a checkbox. Upon some condition, the checkbox should be checked and the row selected.
I tried to do that in createdRow callback:
createdRow: function (row, data, dataIndex) {
if (data.seleccionado) {
$('#chkTrab_' + data.id).prop('checked', true);
$(row).select();
}
}
But that does not work.
How can I do it? Or should I use InitComplete callback by traversing all the rows and selecting them depending on condition of a data in the row?
Thanks
Jaime
This question has an accepted answers - jump to answer
Answers
You probably want to use
rowCallback
which is similar tocreatedCell
but it runs on each table draw for the case where your data can change. If this doesn't help then please provide a link to your page or a test case replicating the issue so we can see exactly what you have.https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case
Kevin
Hello @kthorngren,
I have tried it but that wway I could only check the checkbox, not select the whole row.
Please see this: http://live.datatables.net/jeyesuwo/1/edit
In that sample, at initialization stage I am coloring the first cell of each row and selecting the row. Only the color works.
Regards
Jaime
First you need to install the select.js and css files. Next you need to use an instance of the Datatables API to use the
select()
API. InrowCallback
and many of the other callbacks you can get the Datatatables API instance usingthis.api()
. Also you need to indicate what to select; cell(), column(), row() for example. Please see the updated example:http://live.datatables.net/jeyesuwo/2/edit
Kevin
Thanks! it worked