2014-10-30 13 views
5

tôi sử dụng datatables (http://datatables.net/) để tạo bảng với JSON và tôi có một mã số:Nhận tên cột và hàng khi nhấp chuột vào tế bào trong datatables

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

Làm thế nào tôi có thể nhận được tên của hàng và tên của cột khi Tôi bấm vào một số tế bào? ALso cách nhận ID của hàng và ID cột khi nhấp vào một số ô trong bảng?

Trả lời

10

Tôi nghĩ rằng đây sẽ giúp bạn:

Column Selector

Row Selector

HOẶC

Bạn có thể thử loại mã sử dụng 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()); 
    }); 

Đây là fiddle cho mã JQuery: CLICK HERE

+0

Cảm ơn, điều đó giúp tôi. Có cách nào khác ở đây không? –

+0

Bạn nên giữ dữ liệu hàng bằng 'table.row (this) .data()' thay vì sẵn sàng cho DOM HTML – GETah

Các vấn đề liên quan