Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add row number column to jquery datatables

Tags:

I want jQuery datatables to automatically create row number column in the first column like datagrid in VB.

It looks like this:

enter image description here

Anyone knows how to do this?

like image 920
Xinez Avatar asked Jul 29 '11 09:07

Xinez


2 Answers

Just write a render function:

{
    "data": "id",
    render: function (data, type, row, meta) {
        return meta.row + meta.settings._iDisplayStart + 1;
    }
}
like image 101
Tao Wang Avatar answered Oct 05 '22 12:10

Tao Wang


You just define an empty column in aoColumns.

Then in fnRowCallback function you just edit the column how you like. This callback is run every time new row is created.

Basicly if your first column has the row number, you could just do this in fnRowCallback:

var index = iDisplayIndex +1;
$('td:eq(0)',nRow).html(index);
return nRow;
like image 38
Pehmolelu Avatar answered Oct 05 '22 14:10

Pehmolelu