Destroy and recreate a jQuery Datatables

fefe picture fefe · Nov 24, 2017 · Viewed 20.7k times · Source

I have two jQuery datatables with server side processing. I have a checkbox where I hide and show the two tables. I would like to destroy the none displayed and create another table. How would I do this?

This is what I tried but ajax.reload does not fire.

if ($('#mode').is(':checked')) {
    Table2.ajax.reload();
    Table1.clear();
    Table1.destroy();
} else {
    Table1.ajax.reload();
    Table2.clear();
    Table2.destroy()
}

var Table1 = $('#timesheet-table').DataTable({})
var Table2 = $('#timesheet-table-2').DataTable({})

Answer

Untherxadyus picture Untherxadyus · Nov 12, 2018

In my case, to reset the table I just do this:

$('#table_id').DataTable().clear().destroy();
$('#table_id').empty();

With that you'll reset the table back to it's initial state and you may reinitialize it after that.