How to remove a row before it is rendered

How to remove a row before it is rendered

dduroseddurose Posts: 6Questions: 1Answers: 0
edited August 2016 in Free community support

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

  • Tom (DataTables)Tom (DataTables) Posts: 139Questions: 0Answers: 26
    edited August 2016 Answer ✓

    I suspect too much recursion will come from using draw() along with row().remove() inside createdRow. As you are essentially creating the row then removing it.

    You have a couple of other options that would be better-

    • Filter out the data server-side, although if you are loading the data via a txt file that might not be possible
    • Use ajax.dataSrc to remove items from the array before Datatables renders it, probably your best option
    • Finally, use a custom filtering plugin, that will filter out the rows.

    If you need anymore help let me know

    Thanks

    Tom

  • dduroseddurose Posts: 6Questions: 1Answers: 0

    @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!

  • huiGOdhuiGOd Posts: 1Questions: 0Answers: 0

    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...

This discussion has been closed.