Autoincrementing column in datatable

RIAS
RIAS used Ask the Experts™
on
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
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
ste5anSenior Developer

Commented:
Well, I would add a auto increment or identity column the table.
DarrenSenior Software Engineer

Commented:
Hi,

What database or ORM?

Thanks,

Darren
EE Solution Guide - Technical Dept Head
Most Valuable Expert 2017
Commented:
OWASP: Avoiding Hacker Tricks

Learn to build secure applications from the mindset of the hacker and avoid being exploited.

HuaMin ChenProblem resolver

Commented:
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

Author

Commented:
Thanks

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial