How to disable sort-ability for end-user before ajax.load finished

How to disable sort-ability for end-user before ajax.load finished

vzateychukvzateychuk Posts: 2Questions: 1Answers: 0
edited March 2020 in Free community support

Dear colleagues, could you please to advise:
I having the datatable with data loaded by ajax call . As soon as data returned, new child row created for each parent row. There is a code:
...
function refreshData(url) {
var table = $('#my_table_id').DataTable();
table.ajax.url(url).load(
function() { table.rows().every( this.child( "some child-row data" ).show() ); } );
} ...
Now I facing the problem: this ajax call runs asynchronically. Meanwhile, end-user can re-sort the table during the ajax load. This cause all child rows dissapear from the table mysteriously. I guess this might be some kind of bug in the datatable itself, so I'm trying to resolve this problem by disabling sort-ability for whole table during the ajax load. But all what I see that sortability is options which I can setup only during initialise the table (e.g. columns.orderable).
I wonder if there is any way to disable all columns during ajax load so, end-user won't be able to re-order data.
Could you please show the example if there is a option available to disable/enable column's orderability by API after the table is instantiated?

This question has an accepted answers - jump to answer

Answers

  • colincolin Posts: 15,237Questions: 1Answers: 2,599
    Answer ✓

    There isn't a way to prevent ordering once the table has been initialised - before initialisation you can set ordering. The alternative perhaps would be to remove the listeners on the header elements that cause the ordering to trigger, and then re-add them once the Ajax has completed with order.listener()

    Colin

  • vzateychukvzateychuk Posts: 2Questions: 1Answers: 0

    Hi, Colin, thank you for your answer.
    I wondering how to remove the listeners on the header correctly? Would the removing be something like this code:

    table.order.listener( '#sorter', 1, function() { /*do nothing*/} );
    

    Or there is more elegant and clearer way?

  • colincolin Posts: 15,237Questions: 1Answers: 2,599

    You could do it with $('th').off(), see here - then order.listener() to re-add.

    Colin

This discussion has been closed.