RScott
RScott

Reputation: 53

Destroying Datatable to Reuse

I Have a button on a Data table called "Go Back" which I want to clear and destroy the table ready for it to be reused

I've been able to do this using the clear() and destroy() methods with some success, but after more than one click it stops working

is there a way I can destroy/reload continuously on the same table? or am I barking up the wrong tree?

the code that sort of works is:

{
     text: 'Go Back',
     action: function(e, dt, node, config) {
          $("#table1_wrapper").swap({
               target: "adults",
               speed: 1000,
               opacity: "0.5",
          });
          reportstable.clear().draw();
          reportstable.destroy();
          reportstable.dataTable();
      }
}

Upvotes: 0

Views: 820

Answers (2)

Pankaja Gamage
Pankaja Gamage

Reputation: 304

probably you may have some buttons in your table. If so there is some issue caused by it. If that is the case you can use this.

function destroyDatatable(table) {
            var buttons = [];
            $.each(table.buttons()[0].inst.s.buttons,
                function () {
                    buttons.push(this);
                });
            $.each(buttons,
                function () {
                    table.buttons()[0].inst.remove(this.node);
                });
            table.destroy();
        }

I have used this function and it works fine.

Upvotes: 0

RScott
RScott

Reputation: 53

I have found the answer myself.
The following code worked perfectly

$('#table1').dataTable( {
  "bDestroy": true  
});
$('#table1').dataTable().fnDestroy();
$('#table1').empty();

Upvotes: 1

Related Questions