首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >动态获取DataTables的Id

动态获取DataTables的Id
EN

Stack Overflow用户
提问于 2020-04-20 16:33:13
回答 2查看 146关注 0票数 1

我想要从数据表中提取表的ID,而不指定函数中的表名。例如:我不需要指定var tableName = $('#myTable').attr(' name '),而是想在不编写#myTable的情况下提取表名

代码语言:javascript
复制
$(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.' );
  }

});
EN

Stack Overflow用户

回答已采纳

发布于 2020-04-20 20:03:27

看看https://datatables.net/reference/api/%24.fn.dataTable.tables()

代码语言:javascript
复制
$.fn.dataTable.tables().forEach(function(dt) {
  console.log( dt.id ) //in your case myTable
})

如果你只有一张桌子或者知道该找什么

代码语言:javascript
复制
console.log( $.fn.dataTable.tables()[0].id ) //myTable
票数 1
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61327453

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档