Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make columns unsortable in dataTables jQuery plugin

I am using DataTables jQuery plugin for table representation.

The first column of my table is a row counter and I don't want to sort counter numbers in it. How can I make these column unsortable?

enter image description here

$('#usersTable').dataTable({
        "sDom": "<'row'<'span6'<'#jTableHeader'l>><'span6'f>r>t<'row'<'span6'<'#jTableFooter'>><'span6'p>>",

        "iDisplayLength": 10,
        "aLengthMenu": [[10,25,50,-1],[10,25,50,"All"]],

        "sPaginationType": "bootstrap",
        "aaSorting": [[2, "asc"]],
        "aoColumns": [
            {"bSortable": false},
            {"bSortable": false},
            {"sType": "string"},
            {"sType": "string"},
            {"sType": "string"},
            {"sType": "date"},
            {"bSortable": false}
        ]
    });
like image 410
Dmytro Zarezenko Avatar asked Apr 16 '26 02:04

Dmytro Zarezenko


1 Answers

You cant. The column would be sorted anyway, if you sort one of the other columns.

However, if you just want to maintain the #-order, regardless of the content / sorting of the other columns you could easily mimick the desired behaviour. Eg, I think what you really wants is always to have #1, #2, #3 in the first column, no matter what - right?

Consider this code, using fnDrawCallback to ensure the first column always contain ascending numbers 1..n, a row counter :

var dataTable = $('#example').dataTable({
    aoColumns: [ 
       { "bSortable": false },
       null,
       null,
       null,
       null
    ],
    fnDrawCallback : function( oSettings ) {
       $(this).find('tbody tr').each(function(index) {
         $(this).find('td').first(0).text(index+1);
       });
     }
});

see working fiddle -> http://jsfiddle.net/2bye2/

like image 66
davidkonrad Avatar answered Apr 18 '26 17:04

davidkonrad



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!