6

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?

1 Answer 1

13

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

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, that help me. Is there any other way here ?
You should get hold of the row data by table.row(this).data() instead of readying the HTML DOM

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.