Dim236
Dim236

Reputation: 31

How to get row index in Datatables?

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


            });


        });

Upvotes: 3

Views: 17500

Answers (1)

Rohit Arora
Rohit Arora

Reputation: 2252

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

Upvotes: 3

Related Questions