How to remove a row before it is rendered
How to remove a row before it is rendered
I would like to be able to remove certain rows from my table (based on a certain condition) inside the initialization but I'm not sure how to do it. I know I need to use the row().remove() method (w/ the draw()) in order to do it. I tried removing the row from within the createdRow callback but I'm not sure how to access the row object in order to call the API to remove the row. Here is an example of how I do the initialization (I trimmed some of it out for simplicity sake):
$('#table1').DataTable({
ajax:'reports/test1.txt',
deferRender:true,
fixedColumns:{leftColumns:2},
fixedHeader:true,
scrollX:true,
ordering:false,
'createdRow': function(row, data, dataIndex) {
if (data.toString().indexOf('Total') > -1) {
// add code to remove the row here
}
}
});
I'm not sure how to reference the table object and then access the rows collection in order to remove the given row. I tried the following but that resulted in a "too much recursion" error from jQuery presumably because it's trying to self-reference itself and somehow created an infinite loop or I am referencing the wrong thing:
var myTable = $('#table1').DataTable({
ajax:'reports/test1.txt',
deferRender:true,
fixedColumns:{leftColumns:2},
fixedHeader:true,
scrollX:true,
ordering:false,
'createdRow': function(row, data, dataIndex) {
if (data.toString().indexOf('Total') > -1) {
myTable.rows($(row)).remove().draw();
}
}
});
Thanks in advance for any help you can provide. I'd like to filter out the rows before they get rendered using the API. Right now I'm just using the jquery hide() method on the row but that doesn't properly update the paging and result count because I'm not currently going through the datatables API.
This question has an accepted answers - jump to answer
Answers
I suspect too much recursion will come from using
draw()
along withrow().remove()
insidecreatedRow
. As you are essentially creating the row then removing it.You have a couple of other options that would be better-
ajax.dataSrc
to remove items from the array before Datatables renders it, probably your best optionIf you need anymore help let me know
Thanks
Tom
@Tom (DataTables) Thank you SO much for your reply. I was able to successfully use the ajax.dataSrc option to filter out certain items from the json array data before it's rendered (using the jQuery grep function). You saved me...thank you!
myTable.rows($(row)).remove().draw();
I met you problem above. I find a way you could solute your problem.
myTable.rows(row).remove().draw();
You could try again...