How to sort only certain columns

2019-08-27 14:28发布

问题:

I am using jQuery DataTables plugin to sort table data. I have four columns: Name, Position, Office and Age. What I want is user to be able to sort only Name and Age columns, not all the columns. But I am not able to stop sorting on OTHER columns. Can you please suggest something?

回答1:

This code disable sorting, 0 - index of column. For old version

aoColumnDefs: [
  {
     bSortable: false,
     aTargets: [ 0 ]
  }
]

for DataTables 1.10+

columnDefs: [
   { orderable: false, targets: 0 }
]

Especially for example, you need this

$(document).ready(function() {
    $('#example').dataTable( {
        "order": [[ 0, "desc" ]],
        "aoColumns": [
           null,
           { "bSortable": false },
           { "bSortable": false },
           null
         ]
    } );
} );

fiddle