Javascript 单击数据表中的单元格时获取列名和行名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26646597/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-22 23:11:07  来源:igfitidea点击:

Get column and row name when click on cell in datatables

javascriptjquerydatatable

提问by James D.

I use datatables (http://datatables.net/) for creating table with JSON and I have a code:

我使用数据表(http://datatables.net/)用 JSON 创建表,我有一个代码:

<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?

单击某个单元格时如何获取行名和列名?还如何在单击表格中的某个单元格时获取行 ID 和列 ID?

回答by Jarvis Stark

I think this will help you:

我认为这会帮助你:

Column Selector

列选择器

Row Selector

行选择器

OR

或者

You can try this type of code using JQuery:

您可以使用 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

这是 JQuery 代码的小提琴:单击此处