Thursday, March 2, 2017

Datatables not working with 5 columns or less

I have been working with Datatables plugin on my Laravel 5.2 project.

I'm feeding the table with jQuery and Ajax post.

This is my HTML table:

<table id="entidadeTable" class="table">
     <thead>
        <tr>
           <th>ID</th>
           <th>Nome</th>
           <th>Descrição</th>
           <th>Ações</th>
        </tr>
     </thead>
</table>

And after that I instantiate the table by Datatable:

var entidadeTable = $('#entidadeTable').DataTable({
    "language": {
        "url":"//cdn.datatables.net/plugins/9dcbecd42ad/i18n/Portuguese.json"
    },
    "order": [[0, "desc"]],
    "ajax": {
         "method": "POST",
         "url": "",
         "data": {_token: ""}
    },
    "columns": [
         {"data": "id"},
         {"data": "nome"},
         {"data": "descricao"},
         {"data": "actions"}
    ],
});

This is my controller code, that is responsible to get the data to the datatable:

$entidades = Entidade::all();

return response()->json(['draw' => 1, 'recordsTotal' => $entidades->count(),
            'recordsFiltered' => $entidades->count(), 'data' => $entidades]);

But I'm getting this datatable error:

http://ift.tt/1HY8mfQ

I only get this error if my table has less than 6 columns. I tried to put more than 5 columns on my table and after that it worked and the error disappeared

Error:

DataTables warning: table id=entidadeTable - Requested unknown parameter '4' for row 0, column 4. For more information about this error, please see http://ift.tt/1ieWKe3




via Tiago Elias

Advertisement