2014-10-30 64 views
5

我用用JSON创建表数据表(http://datatables.net/),我有一个代码:获取行和列名时,单击单元格中的数据表

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

回答

10

我认为这将有助于你:

$('#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()); 
    }); 

这就是:

Column Selector

Row Selector

OR

您可以使用JQuery尝试这种类型的代码小提琴JQuery代码:CLICK HERE

+0

谢谢,那帮助我。这里还有其他的方法吗? – 2014-10-30 14:09:53

+0

您应该通过'table.row(this).data()'获取行数据,而不是准备好HTML DOM – GETah 2016-10-21 21:36:17

相关问题