How should a table be destroyed with 1.10?

How should a table be destroyed with 1.10?

jespanajespana Posts: 5Questions: 2Answers: 0
edited November 2014 in Free community support

Hi all,

I have a scenario where my my dataTable will be repopulated with data whenever the user changes the "search" criteria used to populate the table. Depending on the search criteria they select, the data source may come from a different web service. The number of columns and column headers returned will always be the same, but the data itself will be different. Whenever the user changes the criteria, I call

$('#myTable').DataTable().destroy();

I then refresh the data. Most of the time, the data loads fine, but on occasion, I get the following javascript errors. The datatable still loads despite the error. To make it more interesting, 90% of the time that I get the error the table is formatted correctly, but sometimes after getting the error, the "hidden columns" are visible.

FireFox reports:

TypeError: headerCells[i] is undefined
headerCells[i].style.width = column.sWidthOrig !== null && column.sWidthOrig !==...
(error originates from jquer.dataTables.js - line 4089 to be exact)

Chrome and IE report:

Unhandled exception at line 4089, column 5 in http://localhost/xxxxx/Scripts/jquery.dataTables.js JavaScript runtime error: Unable to get property 'style' of undefined or null reference

My question, is if I just want to refresh the data (may require pointing to a different webservice), but the number of columns and column names stay the same, do I need to detroy or do I need to destroy and empty the table? Also, is this a user error or a bug?

Thanks,
JE

Answers

  • allanallan Posts: 63,523Questions: 1Answers: 10,473 Site admin

    Can you link to the page in question showing the error so it can be debugged please? The destroy() method is the correct way to destroy a table. Although I would say you might be better Ajax loading your data and using ajax.reload() if you just want to load different table into the table.

    Allan

  • jespanajespana Posts: 5Questions: 2Answers: 0
    edited November 2014

    Hi Alan,

    Thank you for the response, unfortunately I don't have an external site that I can link to. But I played around with the code a little more and found that if I empty() the table and load it again, it works. Perhaps this is overkill for my usage, but it does give me a reliable result without the errors.

    I will look at the reload method as you suggested though. I think it will be better than what I am doing.

    Thank you again.

    BTW, I love this control...

  • allanallan Posts: 63,523Questions: 1Answers: 10,473 Site admin

    Do the number of columns change? That is the only thing I can think of that might cause this. In which case, empty() probably is the thing to do since columns can't be dynamically added and removed from a table yet.

    Allab

  • jespanajespana Posts: 5Questions: 2Answers: 0

    Nope the number of columns is the same.

This discussion has been closed.