destroy existing datatables

destroy existing datatables

danster3kdanster3k Posts: 3Questions: 0Answers: 0
edited April 2012 in General
Hi guys,
Having an issue with reinitializing a datatable object.
Basically I have a div that loads diferent tables (only 1 visible at a time) via ajax.
All tables have the id="datatable" but are completely different (different columns, type of data, etc)
The first load of a table via ajax works fine.
The issue is when a table gets loaded the second time, the paging and sorting doesn´t show. If i give the tables different ids it works fine. But i want to use the same id on the tables.
I'm guessing I have to destroy the previous datatable instances before creating the new.
I'm trying the following, but no joy:

var table = $.fn.dataTable.fnTables();
if ( table.length > 0 ) {
$(table).dataTable().fnDestroy();
}
$('#datatable').dataTable();

Any help would be appreciated.

Thanks!

Replies

  • danster3kdanster3k Posts: 3Questions: 0Answers: 0
    Sorted it!! The following will do it if anyone else needs it.

    var table = $.fn.dataTable.fnTables();
    if ( table.length > 0 ) {
    $(table).remove();
    }
    $('#datatable').dataTable();
  • allanallan Posts: 63,542Questions: 1Answers: 10,476 Site admin
    That's interesting - I would have expected your original code to work! I've just put it up here as a test: http://live.datatables.net/udawej/edit#javascript,html and it does seem to do the business. Are you using DataTables 1.9.1? The static function fnTables was new in 1.9.1.

    Allan
  • danster3kdanster3k Posts: 3Questions: 0Answers: 0
    Yep using, 1.9.1, and fnDestroy was not working with the hashchange rendering i have setup, but the remove() approach is doing the job nicely.
    Dan
This discussion has been closed.