How to just sort by last datatable column

I am really very new to dataTable and I just need one simple solution:

var initBasicTable = function() { var table = $('#basicTable'); var settings = { "sDom": "t", "sPaginationType": "bootstrap", "destroy": true, "paging": false, "scrollCollapse": true, "order": [[0,'desc']] }; table.dataTable(settings); $('#basicTable input[type=checkbox]').click(function() { if ($(this).is(':checked')) { $(this).closest('tr').addClass('selected'); } else { $(this).closest('tr').removeClass('selected'); } }); } 

This works to sort the first column by default.

But I read that changing 0 in "order": [[0,'desc']] to a negative number will start sorting from the columns on the right. However, this:

 var settings = { "sDom": "t", "sPaginationType": "bootstrap", "destroy": true, "paging": false, "scrollCollapse": true, "order": [[-1,'desc']] }; 

Throws an error, and I do not know where to continue.

I know that dataTable is really powerful and that, but, this is not what I was looking for, but a lot already

Nothing for "Sort by last (-1) column"? I felt lost. anyone?

+4
source share
2 answers

Just use the re-draw function from dataTables:

 table.order([0, 'desc']).draw(); 

And do not use negative values ​​for the column index. just use the positive ones. Here in the api , there is no mention of negative column indices for an ordered column.

If you cannot follow me right now, read: " https://datatables.net/reference/api/order%28%29 "

0
source

It turned out not so difficult, with a little work:

 var table = $('#basicTable'); var index = $(table).find('th:last').index(); var settings = { "sDom": "t", "sPaginationType": "bootstrap", "destroy": true, "paging": false, "scrollCollapse": true, "order": [ [index, "desc"] ] }; 

This will get the index of the last β€œcolumn” and sort it first. Thanks for helping the guys.

+4
source

All Articles