法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()) });