6

I'm really really 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 is working, for sorting the first column by default.

But I read that changing the 0 in "order": [[0,'desc']] into negative number will begin 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 have no idea where to continue.

I know dataTable is really powerful and that but, this is no what I was looking for but plenty already

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

Community
  • 1
  • 1
Fan Zhang
  • 163
  • 1
  • 6
  • Can you specify the last columns index? I.e. if you have 5 columns then it would be 4 – FuzzyTree Aug 03 '15 at 13:19
  • I must have got the idea from reading [aoColumnDefs](http://legacy.datatables.net/usage/columns), I thought [-1] would work since it is acceptable by 'aTarget'. I would want it to always sort the 'last column' no matter on which table. – Fan Zhang Aug 03 '15 at 13:41

3 Answers3

10

It turned out not so hard, with a bit of work around:

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 on it first. Thanks for the help guys.

Fan Zhang
  • 163
  • 1
  • 6
5

You can initialize the table like this to sort it by last column

$('.table').DataTable().columns(-1).order('desc').draw();

for details check this

muzaffar
  • 1,706
  • 1
  • 15
  • 28
1

Just use the re-draw function from dataTables:

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

And don't use the negative values for the column index. just use the positive ones. Here in the api there is no mention about the negative column indexes for orderable column.

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

Luca
  • 1,766
  • 3
  • 27
  • 38
  • I got the idea of negative values from [here](http://legacy.datatables.net/usage/columns) `a negative integer - column index counting from the right`. I know it's not stated in the doc but - why not? If 'aTarget' accepts negative number, generally I would think that 'order' should be the same, right? Otherwise if I apply the script on every page, where tables are shown with different columns, I can't use one script to just sort by the last column. – Fan Zhang Aug 03 '15 at 13:43
  • alright, then... which version of dataTables.net are you using in your project? – Luca Aug 03 '15 at 13:48
  • as you can see that your link in the comment is for older versions than 1.10 you need to use the options provided in the new api – Luca Aug 04 '15 at 06:45
  • Or use: $('#example').dataTable( { "order": [[ 0, 'asc' ], [ 1, 'asc' ]] } ); But than you'll have to prove the positive column index – Luca Aug 04 '15 at 06:47