DataTables獲取指定元素的行數據


法1:

用jquey獲取,var row = $('.edit').parent().parent();

缺點:只能獲取dom上的東西,不能獲取沒有渲染的數據

 

法2:

首先綁定行號到元素上

$('#example').dataTable( {

  "columns": [
      {"data":"name", "orderable": false, "searchable": false,"render" : function ( data, type, row, meta) {
        return  '<button id="btnEdit" data-rowindex="'+meta.row+'">編輯</button>';
      }}
   ] } );

然后根據元素取出行號

var rowIndex = $('#btnEdit').attr('data-rowindex');

最后獲取數據

$('#example').DataTable().rows(rowIndex).data()[0];

 

 如果是單擊選擇行(多選),示例如下:
$(document).ready(function() {
    var table = $('#example').DataTable();
 
    $('#example tbody').on( 'click', 'tr', function () {
        $(this).toggleClass('selected');
    } );
 
    $('#button').click( function () {
        alert( table.rows('.selected').data().length +' row(s) selected' );
    } );
} );

 

如果是單擊單元格獲取數據,示例如下:

//單擊首列,獲取該列中單元格數據
$('#example tr td:first-child').click(function(){ alert($(this).text()) });

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM