Diamonte Diamonte - 10 months ago 64
jQuery Question

jquery datatables static rownumber

I have jquery datatables where first column is rownumber and others are sortable columns. I would like to get this row number as "static", so when sorting table the row number would still keep going 1,2,3,4 no matter how the table is sorted.

now when I sort by other column, the row number changes with the row, ie. 3,1,2,4...


looks like @flawlessdiamond found the solution. I will just post the solution with relevant code for others that might need this answer.

So the solution is this:

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();

Here is also a working fiddle