Link to home
Start Free TrialLog in
Avatar of RIAS
RIASFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Autoincrementing column in datatable

Hi,
How do I add a autonumber  column to the datatable.
example:
Id ColName
1   abc
2   gfg
3   grg
4 ggg

ID should be autoincrementing column.

Thanks
Avatar of ste5an
ste5an
Flag of Germany image

Well, I would add a auto increment or identity column the table.
Hi,

What database or ORM?

Thanks,

Darren
ASKER CERTIFIED SOLUTION
Avatar of M A
M A
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
If you are using SQL server, you can define the column as an identity column, read

https://www.w3schools.com/sql/sql_autoincrement.asp

If you are using Oracle, you can define one sequence for such purpose.
Hi,

Datatables have this https://datatables.net/examples/api/counter_columns.html

You need empty
<th></th> <td></td>

Open in new window

for that column
Then this is the way to use it

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

Open in new window


or if you use
"deferRender": true

Open in new window



$(document).ready(function() {
    var t = $('#example').DataTable( {
        "columnDefs": [ {
            "searchable": false,
            "orderable": false,
            "targets": 0
        } ],
        "order": [[ 1, 'asc' ]]
    } );
                 
    t.on( 'draw.dt', function () {
    var PageInfo = $('#example').DataTable().page.info();
         t.column(0, { page: 'current' }).nodes().each( function (cell, i) {
            cell.innerHTML = i + 1 + PageInfo.start;
        } );
    } );
} );

Open in new window

Avatar of RIAS

ASKER

Thanks