【问题标题】:Get column and row name when click on cell in datatables单击数据表中的单元格时获取列名和行名
【发布时间】:2014-12-26 02:47:33
【问题描述】:

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

【问题讨论】:

    标签: javascript jquery datatable


    【解决方案1】:

    我认为这会对你有所帮助:

    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 代码的小提琴:CLICK HERE

    【讨论】:

    • 谢谢,这对我有帮助。这里还有其他方法吗?
    • 您应该通过table.row(this).data() 获取行数据,而不是准备好 HTML DOM
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多