我有一个DataTables表,我希望能够在单击tr时获得第一个td的值。我已将此td的可见性设置为false。
编辑:因为到目前为止这两个答案假设我可以点击我想要的单元格。 我无法点击我需要的值的单元格。
$(document).ready(function() {
var table = $('#example').DataTable({
select: false,
"columnDefs": [{
className: "ID",
"targets":[0],
"visible": false,
"searchable":false
}]
});//End of create main table
$('#example tbody').on( 'click', 'tr', function () {
cellValue = //code to get the cell value
console.log(cellValue);
});
});
我看过很多使用旧版DataTables方法的示例 fnGetColumnData ,但我不确定如何实现较新的 cell.data()。
任何人都可以帮助我吗?
答案 0 :(得分:5)
通过使用row(this).data()
来实现预期结果并获取隐藏的列数据 $('#example tbody').on( 'click', 'tr', function () {
alert(table.row( this ).data()[0]);
});
http://codepen.io/nagasai/pen/kXyazm
上面提到的代码将返回隐藏和可见数据的完整行数据并提及隐藏列位置的位置