Does destroy() really destroy?

Does destroy() really destroy?

cdamundsencdamundsen Posts: 7Questions: 0Answers: 0

Hi -

I'm having an issue with a DataTable where I create a DataTable that has an unknown number of columns until runtime. The user selects from a list of columns they'd like to see, clicks on a button, the data and header values are fetched via a $.getJSON call and I instantiate the table using a DataTable({"data" : data.data, "columns" : data.headers}) call.

If the user then wants to see a different set of data they can apply different filters and click on the submit button again. I test for existence of the DataTable and if it does I call destroy(). This works fine as along as the user doesn't change the number of columns to be displayed. They can change which columns they want to see as long as the number doesn't change. If they add a column I get "TypeError: n[m] is undefined". If they remove a column I get "TypeError: c is undefined"

I've created a simplified version of what's going on. The "3 Columns #1" and "3 Columns #2" buttons show different tables with three columns. The "2 Columns" button shows a table with 2 columns. Clicking back and forth between "3 Columns #1" and "3 Columns #2" works fine. But clicking on "2 Columns" after having clicked on a 3 Columns button throws an error. Clicking first on "2 Columns" and then on either 3 Columns button also throws an error. The console tab doesn't show the n[m] or c undefinded errors, but instead a more cryptic error.

http://live.datatables.net/cohetayi/1/edit?html,js,console,output

I suspect that the destroy call isn't getting rid of all vestiges of the previous instance of the table. Is there a way to wipe the slate clean so that a new table with a different number of columns can be created?

Thanks,
- Craig

Replies

  • rduncecbrduncecb Posts: 125Questions: 2Answers: 28

    fixed: http://live.datatables.net/muwohepu/1/edit

    I think what it resolves to is datatables creates tr rows in the DOM, when you call destroy with no parameters it destroys the DataTable but leaves the table structure in the DOM. With the number of columns changing in your example the number of tr's may not match the table columns def.

    Calling destroy with a value of is not suitable in your example because it also removes the table node from the DOM and you would have to re-add it.

    You can provide the destroy parameter when creating the table but in my tests it results in the same problem as I think it is just the same as .destroy()/.destroy(false).

    In the end I resorted to emptying the table node in the dom before creating the table to let DataTables recreate all the tr nodes with no chance there could be a mismatch in the number of them.

  • cdamundsencdamundsen Posts: 7Questions: 0Answers: 0

    Thank you so much! This works great.

This discussion has been closed.