在 DataTables 1.10.x 中设置 td id/name 问题

Issue setting a td id/name in DataTables 1.10.x

这与数据表 1 有关。10.x。

我正在使用 this 引用来创建子行,并且很容易将 HTML 放入生成的 javascript 代码中,如下所示:

function format ( d ) {
    return '<div class="slider">'+ 
    '<table id="expandInput" cellpadding="5" cellspacing="0" border="0" style="margin: 0 auto;">'+                
        '<tr>'+
            '<td class="dropHeader">Cost</td>'+
            '<td class="dropInfo"><input required type="text" id="cost" name="cost" value="'+d.cost+'"></input></td>'+                
        '</tr>'+                       
    '</table>'+
   '</div>'; 
}

但这只会影响点击时生成的子项。我不知道如何使用标准数据表语法为数据表本身生成的单元格创建 idname。我能够在数据表的网站上找到的唯一示例涉及使用服务器端

创建 id
var table = $('#ltc-table').DataTable( {    
    "data" : json,        
    "columns" : [
      { data : 'cost' },
      { data : 'resale' }
  ],
  "columnDefs": [
  { className: "details-control", "targets": [ 0 ] }
  ]
});

我知道我可以使用 columnDefs 设置 td 的 class,如 here 所示,但我不知道如何添加其他条件,我需要为生成的每个 td 设置一个唯一的 idname

您需要使用 createdRow 属性 来定义每当为 table 的正文创建 TR 元素时的回调。

$('#example').dataTable( {
   "createdRow": function ( row, data, index ) {
      $('td', row).eq(1).attr('id', 'td-' + index + '-1');
   }
});

代码 $('td', row).eq(1) 用于 select 行中的第二个单元格,使用从零开始的索引(1 用于第二个单元格)。代码 attr('id', 'td-' + index + '-1') 会将单元格 id 属性设置为第一行的 td-0-1,第二行的 td-1-1 等,其中 index 是从零开始的行索引.

请参阅 this JSFiddle or Row created callback example 进行演示。

在我的例子中,它适用于 'rowData'。部分代码:

$('#example').dataTable({
    "columnDefs": [{
        'targets': [4], 'createdCell': function (td, cellData, rowData, row, col) {
            td.id = "ID_For_TD_" + rowData.ObjId;
        }
    }],
});