Update DataTables table after AJax request

0

On my page I have the following:

$(document).ready( function () {
            buscaProdutos();
        } );

When the page loads, it searches the products for listing in the table. Within this function SearchProducts I have the following:

    function buscaProdutos() {
            $.get('buscaProdutos.php',
                function(e){
                    e = $.parseJSON(e);
                    $.each(e, function(indice, produto){
                        $("#produtos").append('' +
                            '<tr>'+
                                '<td>'+produto.codigo+'</td>'+
                                '<td>'+produto.nome+'</td>'+                                
                                '<td>'+produto.data_vencimento+'</td>'+
                                '<td>'+produto.valor+'</td>'+                               
                                '<td >'+produto.status+'</td>'+
                                '<td ><a class="btn">Excluir</a></td>'+
                            '</tr>');
                    });

                    tabela = $('#example').DataTable({
                        "language": {
                            "url": "//cdn.datatables.net/plug-ins/1.10.15/i18n/Portuguese-Brasil.json"
                        }
                    });   
           });
}

In the function that excludes products, after it makes the request to delete I call the searchProducts () function but I get the following error:

  

warning can not reinitialise datatable

I've seen some solutions that use DataTables' own draw to mount the tables but I did not want to have to redo it all since I use it that way in different parts of the system.

The first time I call the search functionProducts gives no error. But from then on it gets this error that it can not reboot the table.

So my question is how can I call DataTables after each search without giving this error?

    
asked by anonymous 02.06.2018 / 05:32

1 answer

0

try this way:

table = $ ('# example'). DataTable ({      "language": {          "url": "//cdn.datatables.net/plug-ins/1.10.15/i18n/Portuguese-Brasil.json"      },      destroy: true });

    
28.09.2018 / 16:46