Reputation: 413
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...
Upvotes: 2
Views: 5405
Reputation: 850
I have another solution.
I use columndef attribute. The solution is simple, i take the current start index value, plus current row number, plus 1. This solution do not need redraw all table each time.
columnDefs: [
{
"targets": 0,
"searchable": false,
"orderable": false,
"data": null,
"title": 'No.',
"render": function (data, type, full, meta) {
return meta.settings._iDisplayStart + meta.row + 1;
}
]
Upvotes: 0
Reputation: 17859
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
Upvotes: 4