Destroying a child row Datatable: $.detach() vs $.remove()?

Destroying a child row Datatable: $.detach() vs $.remove()?

Loren MaxwellLoren Maxwell Posts: 406Questions: 99Answers: 10

Referencing the wonderful "Parent / child editing in child rows" blog post:
https://datatables.net/blog/2019-01-11

Under "Destroying a DataTable", it calls the following code:

function destroyChild(row) {
    var table = $("table", row.child());
    table.detach();
    table.DataTable().destroy();
 
    // And then hide the row
    row.child.hide();
}

I'm not keying in on the purpose of table.detach();.

If I'm destroying the DataTable to prevent a memory leak, doesn't table.detach(); rather than table.remove(); run the same risk?

Or does table.DataTable().destroy(); clean it all up anyway?

And if so, why call table.detach(); or table.remove(); at all?

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,678Questions: 1Answers: 10,497 Site admin
    Answer ✓

    Good spotting! I think we could shorten it to be:

    table.DataTable().destroy(true);
    

    which will remove the table from the document, using jQuery's $().remove() method. That will clean up any event handlers, so yup - go for that:

    function destroyChild(row) {
        $("table", row.child()).DataTable().destroy();
      
        // And then hide the row
        row.child.hide();
    }
    

    Allan

This discussion has been closed.