Best Practices for Efficient DataTable Reuse in Ajax Page

Best Practices for Efficient DataTable Reuse in Ajax Page

gboyskogboysko Posts: 10Questions: 0Answers: 0
edited May 2012 in General
Hello:

I have a pure Ajax application where I load tabular data into a single DataTables instance. The page provides ways to load new data. After each data load, I'd like to reuse the same DataTables instance.

Is the best practice for making this DataTables instance more efficiently reused simply to set "bDestroy" to true?

I am finding that when I do this, the inner table of rows gets narrower (by perhaps 5px each time) each successive reload.

Is it better to blow the entire table away each time (replacing the DOM node with a new empty table)?

Note that there is no prior HTML table that needs to be restored, so this step doesn't even need to be done.

Thanks,
Glenn

Replies

  • allanallan Posts: 63,542Questions: 1Answers: 10,476 Site admin
    > Is the best practice for making this DataTables instance more efficiently reused simply to set "bDestroy" to true?

    No - There is absolutely no difference between fnDestroy and bDestroy - the latter just makes the call to fnDestroy internally. Having said that, if you want to change the initialisation options of the table, then yes, this is required (including changing the columns etc). If you just want to load different data then use the fnReloadAjax plug-in.

    > Is it better to blow the entire table away each time (replacing the DOM node with a new empty table)?

    You need to call fnDestroy or bDestroy since DataTables holds an internal link tot he DOM nodes. If you don't your app will leak memory!

    Allan
  • gboyskogboysko Posts: 10Questions: 0Answers: 0
    One other observation: I am not using an HTML ID to identify DataTables element. Rather, I am using a jQuery selector to find it. This seems to have an impact on the ability of the DataTables to save and find settings (when bStateSave is true).

    It appears that there is a new cookie created each time the DataTables instance is recreated. I think that sInstance may help here. I'm still looking...
  • gboyskogboysko Posts: 10Questions: 0Answers: 0
    Allan:

    Thanks for your prompt reply. In my case, each new tabular data can have different columns. So, I will indeed set bDestroy to true for successive renderings.

    Regards,
    Glenn
  • gboyskogboysko Posts: 10Questions: 0Answers: 0
    Is there a known problem related to table width calculations when re-rendering a DataTables instance? I've other forums talk about adding CSS to set widths to 100%. Does that need to be done after re-rendering?
  • gboyskogboysko Posts: 10Questions: 0Answers: 0
    Last post for the night--I promise. To address the width issue, I am adding code in fnInitComplete to resize the DataTables instance to 100% width.

    Should that be required? Or perhaps there is some bug in my code?

    Thanks for your great work on DataTables and have a great weekend!
  • allanallan Posts: 63,542Questions: 1Answers: 10,476 Site admin
    > Last post for the night--I promise. To address the width issue, I am adding code in fnInitComplete to resize the DataTables instance to 100% width.

    In theory no that shouldn't be required - however applying the original width can be a little tricky since I can only read the pixel value (unless you add the attribute width="100%"). So this is why there might be a problem. I have got a couple of unit tests that should test that, but it is possible that they aren't complete enough tests. If you could provide a test case showing the problem, that would be very useful.

    Allan
This discussion has been closed.