Datatables in Blazor: Dispose() doesn't work

Datatables in Blazor: Dispose() doesn't work

erossinierossini Posts: 4Questions: 3Answers: 0

Following a post on DataTables.net, successfully I added DataTables.net on my Blazor WebAssembly across the application. The problem I'm facing is when I change page. As you can see in the following image, for each page with DataTables.net I change, I still have the search bar from the previous page.

In the code of each page I added

public void Dispose()
{
    JSRuntime.InvokeAsync<bool>("DataTablesRemove", "#tableData");
}

DataTablesRemove is defined in the index.html

function DataTablesRemove(table) {
    $(document).ready(function () {
        $(table).DataTable().destroy();
        // Removes the datatable wrapper from the dom.
        var elem = document.querySelector(table + '_wrapper');
        elem.parentNode.removeChild(elem);
    });
}

How can I fix this issue?

Answers

  • erossinierossini Posts: 4Questions: 3Answers: 0

    I forgot to add at the top of the page

    @implements IDisposable
    
  • allanallan Posts: 61,705Questions: 1Answers: 10,102 Site admin

    Does it work if you do:

    function DataTablesRemove(table) {
      $.fn.dataTable
        .tables( { api: true } )
        .destroy(true);
    }
    

    At the moment you are running the function and then waiting for document ready. I don't know if Blazor fires that multiple times inside a single page does it?

    Allan

This discussion has been closed.