I want to make a function to create a new DataTable. If a table already exists, I would like my function destroy the existing table and create the new one.
I did this:
$.ajax().done(function(response){
Init_DT(response['cols'], response['data']);
});
function Init_DT(cols, data){
if($('#mytable tr').length > 0){
table.destroy();
}
var table = $('#mytable').DataTable({
"data": data,
"columns": cols
});
}
This function works well to initiate my first table but I get "Cannot read property 'destroy' of undefined" on subsequent calls.