How to turn bServerSide on/off programmatically

How to turn bServerSide on/off programmatically

kidalexkidalex Posts: 10Questions: 0Answers: 0
edited November 2013 in General
I have a table that gets re-loaded with data. If I have more than 1,000 I would prefer to turn bServerSide on so I only get 10 records at a time. Otherwise, over 1,000 will slow down or freeze the browser.

Once my JavaScript code is running, it runs a test that determines how many records I'm going to have ( more or less than 1,000 ). And that point, how can I turn bServerSide on or off PROGRAMATICALLY, and then load the table with the data from a DIFFERENT URL. I use fnReloadAjax to change the URL that it uses, but I can't figure out how to toggle the bServerSide flag

This is my current pathetic attempt to do that:

[code]
table.fnReloadAjax({"oFeatures": { "bServerSide": [true|false] }, "sAjaxSource": new_url},function() {
// whatever
});

[/code]

Replies

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    You cannot - when you initialise the table you tell it what mode to operate in. It cannot be changed on-thefly.

    Allan
  • kidalexkidalex Posts: 10Questions: 0Answers: 0
    edited November 2013
    So, what are my choices? Should I remove the table DIV, insert it again and then re-apply the table every time to avoid any kind of memory leaks caused by "lost" listeners and DOM references?
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Basically yes - the bServerSide option can only be set at initialisation time, so you would need to destroy the existing table and create a new one if you want to toggle that option. The reason that it can't be toggled via the public API is that adding that ability would add a considerable amount of code for a feature that would rarely be used.

    Allan
  • kidalexkidalex Posts: 10Questions: 0Answers: 0
    Thank you, Allan. So, do you recommend the JQuery's remove() function to remove the table or is there something better that can clean up everything to avoid memory leaks?
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    The fnDestroy method is available, or bDestroy to reinitialise a table (destroy and then create a new one). These will unbind all of DataTables listeners, although if you added any yourself, they should also be unbound.

    Allan
  • kidalexkidalex Posts: 10Questions: 0Answers: 0
    Allan, so would you recommend to just $('#tables_parent_div').remove() it like that?
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    I'd recommend fnDestroy or bDestroy , although, yes you could use remove() as well - but make sure you destroy the table first, otherwise it will leak RAM.

    Allan
This discussion has been closed.