Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get column and row name when click on cell in datatables

I use datatables (http://datatables.net/) for creating table with JSON and I have a 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 I can get name of row and name of column when I click on some cell ? ALso how to get ID of row and ID of column when click on some cell in table?

like image 510
James D. Avatar asked Oct 30 '14 06:10

James D.


1 Answers

I think this will help you:

Column Selector

Row Selector

OR

You can try this type of code using 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 fiddle for JQuery code: CLICK HERE

like image 53
Jarvis Stark Avatar answered Sep 30 '22 21:09

Jarvis Stark