Remove Rows Before Table Init

Remove Rows Before Table Init

caldjeffcaldjeff Posts: 5Questions: 1Answers: 0

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

  • jvretamerojvretamero Posts: 26Questions: 0Answers: 3

    If you are using the ajax option, you can use the xhr to manipulate the data before DataTables process your data.

    Or maybe use the preInit and manipulate the data with data() (not tested).

  • caldjeffcaldjeff Posts: 5Questions: 1Answers: 0

    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.

  • jvretamerojvretamero Posts: 26Questions: 0Answers: 3

    I hate to have to iterate through all the data again when there are callbacks that already do that

    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 with ajax, and I usually send some additional information to the sever via ajax.data option as a function. I don't know if its your case.

  • kthorngrenkthorngren Posts: 21,303Questions: 26Answers: 4,947
    edited December 2017

    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

  • caldjeffcaldjeff Posts: 5Questions: 1Answers: 0

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

  • kthorngrenkthorngren Posts: 21,303Questions: 26Answers: 4,947
    Answer ✓

    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:

    if ( aScanCult.indexOf(data[1]) > -1 ) $(row).addClass('remove');

    Once the Datatable is initialized you can use the following to remove those rows:

    table.rows('.remove').remove().draw();

    Kevin

  • caldjeffcaldjeff Posts: 5Questions: 1Answers: 0

    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.

  • caldjeffcaldjeff Posts: 5Questions: 1Answers: 0

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

This discussion has been closed.