DataTable 中 CreatedCell 的列名

Column Name on CreatedCell at DataTable

我正在使用 DataTable 并尝试做这样的事情

    columnDefs: [{
      "targets": '_all',
      "createdCell": function (td, cellData, rowData, row, col) {
        alert("You are in column: " + Columns[col].name);
       }
    }]

我需要的是正在创建的单元格所在的列的名称。至于现在我知道 col 有它的索引,但我真的不知道如何从那里开始工作。 rowData 似乎有帮助,因为它具有 table 列的属性,但真的不知道如何使用它。

您可以从 table 的 settings 对象访问此信息 - 但请记住这是一个 内部数据表对象 ,您应该仅在没有其他官方 API 调用时使用,您可以使用。

假设您有一个 table,ID 如下:

<table id="example"></table>

那么你可以使用这个:

columnDefs: [{
  "targets": '_all',
  "createdCell": function (cell, cellData, rowData, rowIndex, colIndex) {
    var colInfo = $('#example').DataTable().settings()[0].aoColumns[colIndex];
    console.log( "This column has the following title: " + colInfo.sTitle );
    console.log( "This column has the following name: " + colInfo.sName );
   }
}]

这显示了如何访问两者 title and name - 选择您需要的一个!

我还没有看到访问其中任何一个的更好方法 - 但如果有的话,那将比这种方法更可取。