javascript - 动态获取数据表的 ID
问题描述
我想从数据表中提取表的 ID 而不在函数中指定表的名称。例如:而不是指定var tableName = $('#myTable').attr('name')我想在不写#myTable的情况下提取表名
$(document).ready(function() {
$('#myTable').DataTable( {
dom: 'Bfrtip',
buttons: [
{ extend : 'collection',
text : '<i class="fa fa-bars">buttons</i>',
buttons: [
'copy', 'csv', 'excel', 'pdf', 'print',
{
text: 'Report Issue',
action: function ( e, dt, node, config ) {
reportIssue(e, dt, node, config);
}
}
]
}]
} );
// e - the button click event
// dt - the datatable object
// node - button node
// config - the button's config (e.g. 'text')
function reportIssue(e, dt, node, config) {
var tableName = $('#myTable').attr('name');
alert( 'The "' + config.text + '" button was clicked\n'
+ 'for the "' + tableName + '" table.' );
}
});
解决方案
使用找到数据表容器dt.table().container()
,然后在其中找到表:
function reportIssue(e, dt, node, config) {
var container = dt.table().container();
var tableName = $(container).find("table").attr('name');
}
推荐阅读
- python-3.x - 如何获取多行中给出的多个整数输入?
- reactjs - 类型错误:arr[Symbol.iterator] 不是函数
- javascript - 选择时输入字段值,不能被覆盖
- kubernetes - Terraform eks 模块 - 工作人员自定义标签
- javascript - 所有子级继承的基类所需的库
- c - 在函数中使用 srand()
- python - 在 Django 模板中有两种不同的形式
- python - Python index of a day-column greater than x in the dataframe of many days
- c - 以下哪些指令可以在非特权模式下运行?1) 加载 2) 存储 3) 输入 4) 输出
- c# - Why Camera.current returns null when I am in Unity Editor? And is there any other ways to catch the current camera?