【问题标题】:Getting column names from DataTables从 DataTables 中获取列名
【发布时间】:2016-12-15 05:23:03
【问题描述】:

我正在动态生成 html 表单中的选择。目的是能够将可见的数据表列换成不可见的数据表列。首先,我不确定如何完成实际的切换。

我的主要问题是如何获取列名。我试过window.table.api().columns()[0];window.table.api().columns().data()[0]; (我知道[0] 索引是如何工作的,[0] 是我表示我将要遍历的方式。有谁知道如何获取这些列的名称?

这是我的构造函数外观的示例:

   window.table =
            $table.dataTable({
                'ajax': {
                    'url': '/api/v1/data',
                    "type": "GET"
                },
                /**
                 * Specify which columns we're going to show
                 */
                columns:             {
                  data: 'Visible',
                  name: 'Visible',
                  visible: true
                  },
                    {
                    data: 'dataName',
                    name: 'Invisible',
                     visible: false
                },
                /**
                 * we want to disable showing page numbers, but still limit the number of results
                 */
                "dom": "t",
                /**
                 * let's disable some dynamic custom css
                 */
                asStripClasses: [],
                /**
                 * let's keep the pages reasonable to prevent scrolling
                 */
                pageLength: 8
            });

【问题讨论】:

标签: jquery datatables datatables-1.10


【解决方案1】:

您可以使用 JQuery selectors 通过 DOM 获取列的名称,而不是使用 Datatables API 来查看这些名称,如下所示:

   //Replace yourTableId with the corresponding Id
    $("#yourTableId thead tr th").each(function(){
        alert(this.innerHTML); //This executes once per column showing your column names!
    }); 

我已针对 https://datatables.net/ 示例表(其中 yourTableId=example)对其进行了测试。

编辑:

由于 OP 说他想找到不可见的列并且这些列不能通过 DOM 直接访问,因此解决方案的结果是这样的:

    //fill with the appropiate constructor
    var table = $('#yourTableId').DataTable();

    //iterate through every column in the table.
    table.columns().every( function () {        
            var visible = this.visible();
            if (!visible)
                alert(this.header().innerHTML);
    });

来源:

columns.every()

column.visible()

问候!

【讨论】:

  • 请看问题,这对隐藏列没有帮助
  • 你的问题不够清楚,能否提供示例代码?
猜你喜欢
  • 1970-01-01
  • 2013-07-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-03
  • 2019-03-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多