Im trying to enable sorting based on one column only in a datatable. but its not working. This is the way I tried
var myTable = $("#tbl_main").dataTable({
"dom": "<'tableinfobar'i><'tablesearchbox'f><'tablestarfilter'><'tablebody't>S<'tablelength'l><'tablepaging'p>",
"ordering": false,
"columnDefs": [{"targets": [0, 6],"searchable": false}, {"targets":2, "type":"html-num","orderable":true}],
"lengthMenu": [
[10, 20, 50, 100, 500, -1],
[10, 20, 50, 100, 500, "All"]
]
}).show();
Here I need to enable sorting only for second column only and tried that in columnDefs
For me the Accepted answer did not work
because in my table all
<th>
has same class and i can't add another class to<th>
and also can not remove default class of<th>
,My table's structure is as below:
Now i also want to enable sort option for
Position
column only.what i have done is as below:
In my
<script>
Now only Position column will be orderable and all others will be not.
So the rule is to put the column first of all which you want to sort in
columnDefs
as above example after it you can put class selector to disable sorting on all other columns.add class
no-sort
to all the<th>
except the column which you want to sort.. kindly check https://jsfiddle.net/24zztcL9/I have enabled sort only for 2nd column "Position"
HTML
jQuery
Rather than using
columnDefs
I prefer usingcolumns
like this:Working JSFiddle
Either approach works, I just prefer the granular control of the
columns
array.