How do I sort my datatable id downwards?

Asked

Viewed 214 times

1

I need to list all id decreasingly on my jQuery datatable.

Which is the best way to it?

<script type="text/javascript">

window.properties = <?= json_encode($ci_properties) ?>;
$(document).ready( function () {
    $('#na_datatable').DataTable();
    $('body').on('click','.btn.view',function(e){
        var id = $(e.target).closest('.btn.view').data("id");
        var property = window.properties.find(p => p.propertie_id == id);
        $('#view-property .modal-body').html('');
        $('#view-property .modal-title .propertie-id').html(property.propertie_id);
        $('#view-property .modal-body').append(`<div><b>Bairro:</b> ${property.bairro}</div>`);
        $('#view-property .modal-body').append(`<div><b>Empreendimento:</b> ${property.empreendimento}</div>`);
        $('#view-property .modal-body').append(`<div><b>Apartamento:</b> ${property.apartamento}</div>`);
        $('#view-property .modal-body').append(`<div><b>Dormtirórios:</b> ${property.dormitorios}</div>`);
        $('#view-property .modal-body').append(`<div><b>Box:</b> ${property.box}</div>`);
        $('#view-property .modal-body').append(`<div><b>Mobiliado:</b> ${property.mobiliado}</div>`);
        $('#view-property .modal-body').append(`<div><b>Chaves:</b> ${property.chaves}</div>`);
        $('#view-property .modal-body').append(`<div><b>Proprietario:</b> ${property.proprietario}</div>`);
        $('#view-property .modal-body').append(`<div><b>Contato:</b> ${property.contato}</div>`);
        $('#view-property .modal-body').append(`<div><b>Valor:</b> ${property.valor}</div>`);
        $('#view-property .modal-body').append(`<div><b>Cadastro:</b> ${property.cadastro}</div>`);
        $('#view-property .modal-body').append(`<div><b>Atualizado:</b> ${property.atualizado}</div>`);
    });
} );

Currently it is like this:

inserir a descrição da imagem aqui

But I always need to list the last record that was registered by first: inserir a descrição da imagem aqui

1 answer

1


To sort the first column in descending order, add the option order at component initialization:

$('#na_datatable').DataTable({
   "order": [[ 0, "desc" ]]
});

The 0 represents the first column.

Browser other questions tagged

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