Get column and row name when clicking on a cell in datatables

I am using datatables ( http://datatables.net/ ) to create a table with JSON, and I have the code:

<script type="text/javascript"> $(document).ready(function() { $('#example').dataTable( { "ajax": "objects.txt", "columns": [ { "data": "name" }, { "data": "position" }, { "data": "office" }, { "data": "extn" }, { "data": "start_date" }, { "data": "salary" } ] } ); } ); </script> <div class="container"> <table id="example" class="table table-striped table-bordered table-responsitive" cellspacing="0" width="100%"> <thead> <tr> <th>DAN</th> <th>Aktivnost</th> <th>Vreme</th> <th>Rashodi</th> <th>Prihodi</th> <th>Beleske</th> </tr> </thead> <tfoot> <tr> <th>DAN</th> <th>Aktivnost</th> <th>Vreme</th> <th>Rashodi</th> <th>Prihodi</th> <th>Beleske</th> </tr> </tfoot> </table> </div> 

How can I get the row name and column name when I click on any cell? Also how to get row id and column id when clicking on a cell in a table?

+5
source share
1 answer

I think this will help you:

Column selector

Row selector

OR

You can try this type of code with jQuery:

  $('#example tbody').on( 'click', 'td', function () { alert('Data:'+$(this).html().trim()); alert('Row:'+$(this).parent().find('td').html().trim()); alert('Column:'+$('#example thead tr th').eq($(this).index()).html().trim()); }); 

This is the script for jQuery code: CLICK HERE

+10
source

Source: https://habr.com/ru/post/1205834/


All Articles