I'm using jQuery DataTables plugin. Sorting works fine, but is there a way to make one column stay always the same no matter what sorting is applied?
For example, the first column are just simple order numbers: 1, 2, 3, 4, 5... And when I sort by date, or anything else, the first column stays in the same order: 1, 2, 3..?
Is there a way to do this? So, I am not trying to disable sorting by first column, but make the first column stay the same when sorting is applied by another columns.
Came across this question while looking for solution for the same problem. Here is a solution as contained in the docs.
$(document).ready(function() {
var t = $('#example').DataTable( {
"columnDefs": [ {
"searchable": false,
"orderable": false,
"targets": 0
} ],
"order": [[ 1, 'asc' ]]
} );
t.on( 'order.dt search.dt', function () {
t.column(0, {search:'applied', order:'applied'}).nodes().each( function (cell, i) {
cell.innerHTML = i+1;
} );
} ).draw();
} );
Basically the index column is re-numbered whenever an order
or search
event is fired. searchable
and orderable
are set to false since they have no effect on the index column (It will still be re-numbered anyways).
You can do that by using orderFixed option which defines ordering that will be always applied to the table.
For example, to always sort the first column in ascending order:
$('#example').dataTable( {
"orderFixed": [ 0, 'asc' ]
} );
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With