how can I set 'ordering' after dt creation?

how can I set 'ordering' after dt creation?

sharpBCDsharpBCD Posts: 12Questions: 3Answers: 0

I want to start with a dt with ordering set to false and turn it after pageload.
I have, but it's not working:

const dt_1 = $('#my_id').DataTable({
   ordering  : false,
//...... others
});
// and then
$(() => {
    dt_1.ordering = true;
    dt_1.draw();
}

This question has an accepted answers - jump to answer

Answers

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

    Hi @sharpBCD ,

    Yep, as you say, that won't work. Many initialisation options can't be changed later, and ordering is one of those. What you could do is set the initial order to nothing, with order, then change it to the order you want later with order(). Not quite the same, but hopefully good enough,

    Cheers,

    Colin

  • sharpBCDsharpBCD Posts: 12Questions: 3Answers: 0

    Hi Colin,
    The idea is to avoid parsing the data set for ordering to speed up first-time load page. After the page is loaded, I can spare 1sec because the user will be busy checking out data so won't notice background calculations.

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

    Hi @sharpBCD ,

    DataTables is very fast at that initial ordering - if that's the criteria, it would be worth profiling it (if you haven't already) to see how much different it will make. The workaround I mentioned above should do the trick for you, since with ordering set to [], it will just display the data in the order it was loaded,

    Cheers,

    Colin

  • sharpBCDsharpBCD Posts: 12Questions: 3Answers: 0

    It is fast, indeed, but I'm loading a small img (country flag) on each column. This takes a few ms/img as it is downloaded on request when the page is first accessed. After caching is not a big deal anymore first impression always matter a lot. :smile:

  • allanallan Posts: 63,542Questions: 1Answers: 10,476 Site admin

    Perhaps you could load the flags as sprites. That should significantly improve initial performance since it wouldn't need multiple requests to get the files (unless you are using http/2 where it wouldn't make that much difference).

    Allan

This discussion has been closed.