Reinitialize datatables

Reinitialize datatables

islamelshobokshyislamelshobokshy Posts: 99Questions: 20Answers: 1
edited May 2018 in Free community support

I have got

if ($.fn.DataTable.isDataTable("#achats_table")) {
    $('#achats_table').DataTable().clear().destroy();
        $('#achats_table').empty();
}

with destroy: true but datatables doesn't want to get destroyed. The first time it's created, second time the number of rows double for a reason....

Replies

  • kthorngrenkthorngren Posts: 21,246Questions: 26Answers: 4,929

    Your code seems to work here:
    http://live.datatables.net/merufote/1/edit

    Works with or without destroy: true.

    Maybe you can work up a test case showing the issue.

    Kevin

  • islamelshobokshyislamelshobokshy Posts: 99Questions: 20Answers: 1

    Then try to recreate a datatable after it has been deleted. That doesn't work.

  • kthorngrenkthorngren Posts: 21,246Questions: 26Answers: 4,929

    Are you recreating the HTML table after this:
    $('#achats_table').empty();

    If not then Datatables won't have a table to use.

    Kevin

  • islamelshobokshyislamelshobokshy Posts: 99Questions: 20Answers: 1
    edited May 2018

    I do, of course recreate it. But it creates it next to my current one. So first time I had 5 columns/headers, next time I get 10. As if the clear/destroy doesn't work.

    $.ajax({
        type: "GET",
        dataType: "json",
        url: '/api/purchase?'+url,
        success: function (data) {
            //Getting data variable containing
            data = data.data;
            if ($.fn.DataTable.isDataTable("#achats_table")) {
                $('#achats_table').dataTable().clear().destroy();
                $('#achats_table').empty();
            }
                    var oTable = $('#achats_table').DataTable( {
                destroy: true,
                            // stuff
            }
    });
    
    
  • kthorngrenkthorngren Posts: 21,246Questions: 26Answers: 4,929

    The basic example I provided works so there must be something more in your environment causing the issue. Can you provide a test case?

    You can use one of the ajax based jsbin templates to build your ajax portion of the test case:
    https://datatables.net/manual/tech-notes/9#Ajax

    Kevin

This discussion has been closed.