destroy() when using language file

destroy() when using language file

hdohdo Posts: 3Questions: 1Answers: 0

Calling $("#example").DataTable().destroy();when a language file is used causes the error
Cannot read property 'parentNode' of null and the destruction does not work.

A small example is here: http://live.datatables.net/selovibu/1/edit
You can see the detailed error message in the JS-Console of your Browser, and also that the destroy call has no effect (the data-table remains).

Answers

  • allanallan Posts: 61,665Questions: 1Answers: 10,096 Site admin

    Hi,

    This is actually expected and noted in the language.url documentation:

    Note that when this parameter is set, DataTables' initialisation will be asynchronous due to the Ajax data load. That is to say that the table will not be drawn until the Ajax request as completed. As such, any actions that require the table to have completed its initialisation should be placed into the initComplete callback.

    Dropping the destroy call into initComplete will allow it to work as expected.

    Allan

  • hdohdo Posts: 3Questions: 1Answers: 0

    Thanks for your answer, i actually didn't notice that section in the docs.

    But, it does not solve my problem (I think). In my special case, I need to recreate an existing DataTable, which normally can easily be done with destroy. I used the example from your docs and added a language-url: http://live.datatables.net/kojutero/1/edit

    That fails also with the described error and I can't image how initComplete can help in this case.

  • allanallan Posts: 61,665Questions: 1Answers: 10,096 Site admin

    The comment says - some time later. What triggers that? The issue is happening in that code segment because the first FataTable is still initialising. If you moved that code into a button click handler, which might be more realistic, it should work okay.

    Allan

  • hdohdo Posts: 3Questions: 1Answers: 0

    I use the datatable with react.js (the combination of both is pretty easy when dealing appropiate with the react lifecycle). Therefore react "decides" when the table should be recreated.

    I can work around by setting the different language properties directly... little bit annoying but works.
    What really confuses me is that I can use valid options when creating the table (destroy and language-url), and afterwards it is expected that this combination failes with an cryptic error message which does not lead to the problem.

    But anyway, thanks for your help.

  • allanallan Posts: 61,665Questions: 1Answers: 10,096 Site admin

    Hi,

    This is an example of how it can work: http://live.datatables.net/kojutero/2/edit .

    I think it sounds like React is triggering another DataTable create before the old one has finished, which results in the error you are seeing.

    I need to look into React integration a bit more!

    Allan

This discussion has been closed.