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

2
  • you're looking for something like this or this Commented Dec 4, 2020 at 16:19
  • Thanks for your input @Pirate but this does not work with what I am trying to do. Thanks Commented Dec 5, 2020 at 11:42

1 Answer 1

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>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.