1

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.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Ganesh
  • 505
  • 2
  • 9
  • 26
  • there is an option to select all columns. You can try with s.th like that: `{ "aTargets": [ '_all' ], "bSortable": false }` – Markus Kösel Nov 20 '13 at 04:08
  • @MarkusKösel :that will remove sorting for all columns, i want sorting on only col1 and col2. – Ganesh Nov 20 '13 at 04:12

1 Answers1

7

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

Community
  • 1
  • 1
Markus Kösel
  • 753
  • 1
  • 6
  • 17
  • no luck, i guess since we are using { aTargets: [ '_all' ], bSortable: false } it will disable sorting for all columns. – Ganesh Nov 20 '13 at 04:28
  • hm, i thought, that with the other two lines, column 1 and 2 should be enabled again. bSort should be enabled. I accidentaly commented it out in my example – Markus Kösel Nov 20 '13 at 04:36