Search code examples
jquerysortingsearchdatatablesjquery-callback

Disable/Prevent search callback when inserting new row with jquery Datatable


Background:

I have a Datatable that I am creating like this in JS:

let mytable = $('#myTableId').DataTable();

Then later on in my code, I am programmatically adding 5 rows to this datatable like this:

for(let i=0; i<5; i++){
    mytable.row.add(someVectorValuedFunction(i)).draw(false);
}

Problem:

Every time a row is inserted into the datatable 'mytable', the datatable sorting function is called. The sorting function runs is O(n) - ie, it iterates through every row and decides whether to show that row or hide it. Therefore inserting n rows into 'mytable' has an effective worst case complexity of O(n^2). The exact complexity is something like n(n-1) becomes of the running aggregation of sort cycles.

Constraint:

I need to be able to sort the table at some point. This means that whatever solution I find must be consistent with the table's ability to sort OR I need to be able to disable and then enable sorting on the table.

Question:

How do I prevent the sorting function from being called every time a row is inserted into a jQuery Datatable?


Solution

  • Just change the draw(false) -> draw('page'). This should stop the draw function from reordering and resorting according to the documentation.

    https://datatables.net/reference/api/draw()