Cannot reinitialise JQuery DataTable

前端 未结 9 543
北恋
北恋 2020-12-13 19:32

I\'m using jquery datatables to display data inside grid. On init page load script take DateTime.Today and process them further, problem is after init page load, when I\'m t

相关标签:
9条回答
  • 2020-12-13 19:48

    Try adding "bDestroy": true to the options object literal, e.g.

    $('#dataTable').dataTable({
        "bServerSide": true,
        ....
        "bDestroy": true
    });
    
    0 讨论(0)
  • 2020-12-13 19:48

    The DataTables API has changed, but this error is still thrown if you try to reinitialize the datatable again.

    You can check if it is already created with:

    $.fn.DataTable.isDataTable("#myTable")
    

    And to destroy it so that it can be recreated again:

    $('#myTable').DataTable().clear().destroy();
    

    It is not the most efficient way, but it works. It should be possible to update the table without destroying it first, just using clear and row.add, but I haven't found a way of doing that when the data source is an array passed to the constructor.

    0 讨论(0)
  • 2020-12-13 19:51

    The first thing you wanna do is to clean and destroy your datatables.

    var tables = $.fn.dataTable.fnTables(true);
    
    $(tables).each(function () {
      $(this).dataTable().fnClearTable();
      $(this).dataTable().fnDestroy();
    });
    

    and then re-create.

    $("#datagrid").dataTable();
    
    0 讨论(0)
  • 2020-12-13 19:56
    var myDataTable = $('#dataTable').DataTable();
    myDataTable.ajax.reload();
    

    Absolutely this is what I am looking for.Nice solution to reintialise datat table

    0 讨论(0)
  • 2020-12-13 20:05

    I know this is an OLD question. But this is for anyone else having similar issue.

    You should destroy the old dataTable assignment. Before creating the new datatable use the following code

    $("#dataTable").dataTable().fnDestroy();
    
    0 讨论(0)
  • 2020-12-13 20:05

    Clear the existing dataTable:

    $(this).dataTable().fnClearTable();
    $(this).dataTable().fnDestroy();
    
    0 讨论(0)
提交回复
热议问题