3

I would appreciate any help in ammending my script to add row call back function in database tables.

Using datatables I want to show the row number. I have found the code provided by @Pehmolelu in answer to a similar question but as this is my very first attempt with databasebles and javascript. I do not know enough about the syntax to put them together. The script I am current using:

<script> type="text/javascript">
                
          $(document).ready(function(){
                $('table').DataTable({          
                   searching:true, 
                   ordering:false,
                   paging:true,
    "bLengthChange": false,
                   lengthMenu:[31], 
 
                })
            });
</script>

This is what I think will show the row number provided by @Pehmolelu:

var index = iDisplayIndex +1;
$('td:eq(0)',nRow).html(index);
return nRow;

And I know it involves adding:

"fnRowCallback": function( nRow, aData, iDisplayIndex )

I have also tried this code, but this code shows a the row number for every row, so if there are a hundred rows it will show 1-100. What I want is for it to work with the pagination. So, if set to 20 rows per page each page would show row 1-20.

$(document).ready(function() {
    var t = $('table').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();
} );

I know it is very basic but I would really appreciate any help. Thanks

  • you're looking for something like [this](https://stackoverflow.com/questions/27039194/select-all-rows-in-active-page-only-jquery-datatable) or [this](https://datatables.net/forums/discussion/34672/how-do-i-loop-through-rows-on-current-page-only) – Pirate Dec 04 '20 at 16:19
  • Thanks for your input @Pirate but this does not work with what I am trying to do. Thanks –  Dec 05 '20 at 11:42

1 Answers1

0

In case it helps anyone else and studying the info from @Pirate here is what I found works.

<script> type="text/javascript">
                
$(document).ready(function(){
                $('#table').DataTable({
                    
                   searching:true, 
                   ordering:false,
                   paging:true,
                   "bLengthChange": false,
                   lengthMenu:[31], 
                "fnRowCallback": function (nRow, aData, iDisplayIndex) {
 var index = iDisplayIndex +1;
$('td:eq(0)',nRow).html(index);
return nRow;
}   

                });
                
            });

</script>