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

标签 javascript jquery datatable

我使用数据表 ( 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>

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

最佳答案

我认为这对你有帮助:

Column Selector

Row Selector

您可以使用 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());
    });

这是 JQuery 代码的 fiddle :CLICK HERE

关于javascript - 单击数据表中的单元格时获取列名和行名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26646597/

相关文章:

javascript - CSS :not selector act strangely on section tag

javascript - 如何从 PHP 文件获取 ajax 结果值

nhibernate - 是否可以使用 NHibernate 调用存储过程并填充数据表?

javascript - 将 2D 数组拆分为 3D 数组 JavaScript

javascript - 范围包含值,但访问它时,它是未定义的

javascript - jquery 字符串的所有排列

javascript - 重新加载以显示新保存的记录时,数据表未正确重新初始化

javascript - "this"在 map 运算符 Angular 中未定义

javascript - 每次 iframe 重新导航时执行操作。 (javascript)

javascript - 仅刷新数据表页面时如何清除状态