您的位置:首页 > 其它

DataTables获取指定元素的行数据

2017-07-06 15:55 197 查看
法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];

如果是单击单元格获取数据,示例如下:

//单击首列,获取该列中单元格数据
$('#example tr td:first-child').click(function(){ alert($(this).text()) });
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: