Destroying a datatable without restoring/redrawing the original

Destroying a datatable without restoring/redrawing the original

jerrosenbergjerrosenberg Posts: 7Questions: 0Answers: 0
edited August 2011 in General
Calling fnDestroy restores and redraws the original table it was based on. Is there a way to prevent this? In our case, we call fnDestroy on datatables before we remove them from the DOM to prevent leaking the element in the $.fn.dataTableSettings array. We really only need the line of fnDestroy that splices that array. The rest of it is just a performance hit right now, and it's very noticeable.

If there is no way to do this now, can you maybe add an option to fnDestroy called restoreOriginal or something? If we pass false, then all it would do is unwire the DT events and kill the settings reference?

Replies

  • fbasfbas Posts: 1,094Questions: 4Answers: 0
    not sure, but I think calling $('#yourtable').dataTables({ bDestroy: true }); will work for you. it should attach to an existing table and destroy it's contents.
  • allanallan Posts: 63,523Questions: 1Answers: 10,473 Site admin
    When destroying the table, you will need to do more than just removing it from the settings array and removing it from the DOM - for example removing all the jQuery event handlers, which would leak like crazy if that wasn't done.

    One option might be to copy the fnDestory function and trim it down to do just what you need only, and not restore the table to its full glory in plain HTML. This could be wrapped up in a plug-in API method.

    Allan
  • fbasfbas Posts: 1,094Questions: 4Answers: 0
    good point. use jquery's .remove() http://api.jquery.com/remove/
  • jerrosenbergjerrosenberg Posts: 7Questions: 0Answers: 0
    Yeah, sorry I wasn't clear - that's what I meant by unwiring the DT events when called restoreOriginal set to false. This would be part of the general solution.

    In our app, this would be redundant, because we are hooking jquery's cleanData to call fnDestroy, so you are already in the process of a .remove, which will kill the events anyway.

    I will try to put this into a plug-in. I just wasn't sure if this is a common case, if you'd want this in the main library. Plus then we wouldn't have to maintain it between versions :)

    Thanks.
This discussion has been closed.