Attempting to auto select rows on table creation
Attempting to auto select rows on table creation

I'm attempting to auto select certain rows based on data I'm getting from an API inside the createdRow
function, it does not seem to be working. Should I be doing this somewhere else?
createdRow: function (row, data, dataIndex) {
// Create custom column design
for (var i = 0; i < selectedPorts.length; i++) {
if (selectedPorts[i].PortID == data.PortID) {
groupTable.row(`:eq(${dataIndex})`).select();
break;
}
}
}
if I use groupTable.row(':eq(0)').select();
after the table has been created, it works fine (with a number and not dataIndex)
This question has an accepted answers - jump to answer
This discussion has been closed.
Answers
Using
createdRow
is probably too early to select rows. You are probably getting errors indicatinggroupTable
is undefined because initialization is still in progress. I would userowCallback
. You can get an instance of the Datatables API and userows().every()
to iterate all the rows. Here is a simple example:http://live.datatables.net/pevonawu/1/edit
Kevin
How are you adding the row? If you are using
row.add()
orrows.add()
they are auto-populated with a selection for the new rows. So you can dotable.row.add(...).select();
.Allan
Thanks kthorngren, I did not use the rowCallback, but the drawCallback in the example you showed and it worked well.