Remove Rows Before Table Init
Remove Rows Before Table Init
I'm storing some data on the client (localStorage) that contains a list of ids that should be excluded from the table on initialization. I don't have the ids server-side, so this has to happen on the client. I thought this would be simple by using the rowCallback, but I can't seem to figure out how to remove the row if it's in my list of excludes. Here's my best attempt so far. I don't get any errors, just nothing happens and the rows are still there:
rowCallback: function( row, data, index ) {
if ( aScanCult.indexOf(data[1]) > -1 ) row.remove();
}
Surely this can be done before the table builds rather than after it's all ready, right?
This question has an accepted answers - jump to answer
Answers
If you are using the
ajax
option, you can use thexhr
to manipulate the data beforeDataTables
process your data.Or maybe use the
preInit
and manipulate the data withdata()
(not tested).Thanks @jvretamero unfortunately, not using ajax. Data comes from js array built server-side and sent in page. I'll look at preInit, but I hate to have to iterate through all the data again when there are callbacks that already do that. But again, maybe I'm missing something here.
Yeah, me too, but it seems that
DataTables
expect the data to be already filtered. This seems like a nice feature request, some kind of "real-time" filter.I use
DataTables
withajax
, and I usually send some additional information to the sever viaajax.data
option as a function. I don't know if its your case.The
rowCallback
callback only affects the rows that are displayed, not all rows. Not sure this callback will meet your requirements of removing all the matching rows from the table.initComplete
is another option to look at.Or as @jvretamero suggested you could manipulate your data before being applied to Datatables.
I don't believe there is a callback that will affect all Datatables rows.
Kevin
@kthorngren I'm only worried about rows displayed and, in fact, have no pagination and limited filtering even available. I just can't figure out how to actually remove the row from the rowCallback. That was my original question. Is this possible?
I don't know of a way to remove rows from within
rowCallback
. @allan can say for sure.However you could set a class for each row you want to remove in
rowCallback
, for example:Once the Datatable is initialized you can use the following to remove those rows:
Kevin
Thanks @kthorngren not a bad fallback option. I'm hoping @allan will chime in with a definitive answer on whether or not this is possible.
@kthorngren Kevin, I ended up going with your solution. It still feels a little like a workaround and I'm sure @allan would have a cleaner way to do it without having to build the entire table, render it and then remove, but this works. Thanks again for the idea.