How to solve this problem in Datatables?

Asked

Viewed 970 times

0

I have a problem with Datatables that is keeping me awake. I have the following code. It even shows the datatable but the table functions don’t work. Next doesn’t even show the Prev. If I remove the Stroy it comes back in the message: Warning: Cannot reinitialise Datatable. What can be?

 function retorna_cliente()
 {
 //  $('.mostra_clientes .table').DataTable().destroy();
   var oTable = $('.mostra_clientes .table').DataTable({
  "pageLength": 4,
  "ajax": {
    "url": url_base + "clientes",
    "type": "GET",
    "dataSrc": "",
  },
  "columns": [
    {
      "data": function ( data, type, row ) {
        return "<input type='checkbox' value='"+data['id']+"' name='verifica_check_box[]' id='verifica_check_box' class='flat'/>";
      }
    },
    { "data":"nome"},
    { "data":"data_nascimento"},
    { "data":"telefone"},
    { "data":"celular"},
    { "data":"cpf"},
    { "data":"endereco"},
    { "data":"email"},

  ], language: {
    "sProcessing":   "Carregando...",
    "sLengthMenu":   "Mostrar _MENU_ registros",
    "sZeroRecords":  "Não foram encontrados resultados",
    "sInfo":         "Mostrando de _START_ até _END_ de _TOTAL_ registros",
    "sInfoEmpty":    "Mostrando de 0 até 0 de 0 registros",
    "sInfoFiltered": "(filtrado de _MAX_ registros no total)",
    "sInfoPostFix":  "",
    "sSearch":       "Buscar:",
    "sUrl":          "",
    "oPaginate": {
      "sFirst":    "Primeiro",
      "sPrevious": "Anterior",
      "sNext":     "Seguinte",
      "sLast":     "Último"
    }
  },
  //"bDestroy": true,
});

oTable.ajax.reload(null, false);

oTable.destroy();

}

  • I suggest creating two methods, one to mount the table with the . Datatable and the other only to reload the data with ajax.reload. When the command . Datatable is called in an existing table, this error happens. My table was not ajax, so what I did was remove it completely and mount from scratch by calling . Datatable again.

1 answer

0

As specified in official documentation, this error occurs when we try to restart an already started table. By default, Datatables does not let us change a table that has already been created, but there is the configuration Destroy, that can be passed as true when starting the table, thus:

$('#exemplo').DataTable({
  destroy: true,
  paging:false
});

This setting causes the current table to be destroyed before a new one is created.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.