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.

2
  • there is an option to select all columns. You can try with s.th like that: { "aTargets": [ '_all' ], "bSortable": false } Commented Nov 20, 2013 at 4:08
  • @MarkusKösel :that will remove sorting for all columns, i want sorting on only col1 and col2. Commented Nov 20, 2013 at 4:12

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

2 Comments

no luck, i guess since we are using { aTargets: [ '_all' ], bSortable: false } it will disable sorting for all columns.
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.