Date sorting problem with DATATABLE plugin

Asked

Viewed 80 times

0

Friends, I am having trouble sorting a table with Datatable plugin and I do an SQL query already ordered by a field and when it presents the table with the plugin it reorders otherwise and I have tried several unsuccessful ways to solve it.

SQL

$sql  = "SELECT * FROM $this->table ORDER BY date(data_cadastro) DESC";

Datatable

var tabela = $('#Compras').dataTable( {
            "aoColumns": [  null,null,null,null,null,{ "bSortable": false },{ "bSortable": false },{ "bSortable": false },{ "bSortable": false },null,{ "bSortable": false } ],

            "oLanguage": {
            "sEmptyTable": "Nenhum registro encontrado",
            "sInfo": "Mostrando de _START_ até _END_ de _TOTAL_ registros",
            "sInfoEmpty": "Mostrando 0 até 0 de 0 registros",
            "sInfoFiltered": "(Filtrados de _MAX_ registros)",
            "sInfoPostFix": "",
            "sInfoThousands": ".",
            "sLengthMenu": "_MENU_ resultados por página",
            "sLoadingRecords": "Carregando...",
            "sProcessing": "Processando...",
            "sZeroRecords": "Nenhum registro encontrado",
            "sSearch": "Pesquisar",
            "oPaginate": {
                "sNext": "Próximo",
                "sPrevious": "Anterior",
                "sFirst": "Primeiro",
                "sLast": "Último"
                }
            },
            "oAria": {
                "sSortAscending": ": Ordenar colunas de forma ascendente",
                "sSortDescending": ": Ordenar colunas de forma descendente"
            }

            });

        })

PHP

<?=date("d/m/Y", strtotime($x->data_cadastro)) ?>

Sql return:

14/03/2018
14/03/2018
14/03/2018
13/03/2018
12/03/2018

Table result:

14/03/2018
14/03/2018
13/03/2018
12/03/2018
14/03/2018

2 answers

0

Thiago, the ordering in the datatable is by default (year-month-day).

In the column containing the date, you can do:

<tr>
    <td data-order="DATAPADRAO">DATA_BR</td>
</tr>

More info

0

Faced with several attempts I found a pleasant solution, but generates an error that I could not decipher yet, but the result is what I need.

"aaSorting": [[6, 'DESC']],

this shape I can sort by column number the date format

"order": [[6, 'DESC']],

doesn’t work.

the error generated

jquery.dataTables.min.js:84 Uncaught TypeError: p[((intermediate value)(intermediate value)(intermediate value) + "-" + o[c][1])] is not a function

someone knows how to help me delete this mistake !!!

Browser other questions tagged

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