Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DataTable sorting: make one column stay fixed

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.

like image 745
kasijus Avatar asked Oct 19 '22 06:10

kasijus


2 Answers

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).

like image 153
inginia Avatar answered Oct 27 '22 10:10

inginia


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' ]
} );
like image 40
Gyrocode.com Avatar answered Oct 27 '22 09:10

Gyrocode.com