2

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

3
  • jsfiddle.net/6r4bfh4L/8 added where trying to run some tests Commented Oct 30, 2015 at 9:12
  • solved, I don't know yet the details and logic behind this but datatables.net/examples/api/counter_columns.html has the solution, I were looking with wrong keywords. Commented Oct 30, 2015 at 9:20
  • 1
    Please post your solution as an answer not as a comment. Commented Oct 30, 2015 at 10:20

2 Answers 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

Sign up to request clarification or add additional context in comments.

1 Comment

Excellent. This helped in my project :)
0

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; 
            }
]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.