Ganesh
Ganesh

Reputation: 515

How sort with specific column in Datatables?

I am using Datatables to print HTML tables,here i am stuck with something like:

table is dynamically created (printed with while loop) due to which I don't know how many columns will it have. After that I applied datatables on

$('#table').dataTable( {
            "bDestroy":true,        
            "sScrollY": temp_fh,             
            "bPaginate": false,
            "bScrollCollapse": true,
            "bProcessing": true,
            "bFilter":true,
            "bSort":true,
                  });

So now how do I apply sorting for only 2nd column?

Since I referred bSortable from Datatables which allows us to disable sorting for particular column but in this case we don't known how much columns will table have.

Thanks.

Upvotes: 1

Views: 13333

Answers (1)

Markus Kösel
Markus Kösel

Reputation: 753

how about this?

$('#table').dataTable( {
        "bDestroy":true,        
        "sScrollY": temp_fh,             
        "bPaginate": false,
        "bScrollCollapse": true,
        "bProcessing": true,
        "bFilter":true,
        "bSort":true,
        aoColumnDefs: [
           { aTargets: [ '_all' ], bSortable: false },
           { aTargets: [ 0 ], bSortable: true },
           { aTargets: [ 1 ], bSortable: true }
        ]

Update

ok, overriding _all seems not possible. maybe you can disable sorting on columns by css like this in your while-loop:

<th class="no-sort">

and use this initialization code:

// Disable sorting on the no-sort class
"aoColumnDefs" : [ {
"bSortable" : false,
"aTargets" : [ "no-sort" ]
} ]

see: disable a column sorting using jquery datatables

Upvotes: 7

Related Questions