How to get row index in Datatables?

10,382

EDIT:

Check out this link Datatables-addrow and code is here:

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

For getting index in jquery :

$('#example tbody').on( 'click', 'tr', function () {
    alert( 'Row index: '+table.row( this ).index() );
} );

For more information visit Datatables

Share:
10,382
Dim236
Author by

Dim236

Updated on June 04, 2022

Comments

  • Dim236
    Dim236 almost 2 years

    I used following javascript to get row index. By using this the ID(table header) always starts with 1 in case next 10 values are selected in every page.i need to show next 10 as 11 to 20. Thank you very much..

      $(document).ready(function() {
                 $('#example').dataTable( {
                    "processing": true,
            "serverSide": true,
                    "ajax": "scripts/server_processing.php",
    
                    "aoColumns": [
                        {
                            "sTitle": "ID"},
    
                        {
                            "sTitle": "E Mail"},
                        {
                            "sTitle": "FirstName"},
                        {
                            "sTitle": "LastName"},
                        {
                            "sTitle": "Company"},
                        {
                            "sTitle": "Course"},
                        {
                            "sTitle": "Module"},
                        {
                            "sTitle": "Completions"},
                        {
                            "sTitle": "First"},
                        {
                            "sTitle": "Last",
                            "sClass": "center"},
                        {
                            "sTitle": "Lowest"},
                        {
                            "sTitle": "Highest",
                            "sClass": "center"},
    
    //                    {
    //                        "sTitle": "#"},
                    ],
    
                     "fnRowCallback": function( nRow, aData, iDisplayIndex, iDisplayIndexFull ) {
                         var index = iDisplayIndex +1;
                         $('td:eq(1)',nRow).html(index);
                         return nRow;
                     }
    
    
                });
    
    
            });
    
  • Dim236
    Dim236 over 8 years
    Thanx but I want to show row index column in table.
  • Rohit Arora
    Rohit Arora over 8 years
    Match your html code, javascript code properly. any errors in console ?
  • Dim236
    Dim236 over 8 years
    <table id="example" class="display select" cellspacing="0" width="100%">
  • Rohit Arora
    Rohit Arora over 8 years
    @Dim236 It works for all in the demo. You must recheck it once more.