这与数据表 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>';
}
但这只会影响点击时生成的子项。我不知道如何使用标准数据表语法为数据表本身生成的单元格创建 id
或 name
。我能够在数据表的网站上找到的唯一示例涉及使用服务器端创建 id
var table = $('#ltc-table').DataTable( {
"data" : json,
"columns" : [
{ data : 'cost' },
{ data : 'resale' }
],
"columnDefs": [
{ className: "details-control", "targets": [ 0 ] }
]
});
我知道我可以使用 columnDefs
设置一个 td
类,如 here 所示,但我不知道如何添加其他条件,我需要为生成的每个 td 设置唯一的 id
和 name
。
最佳答案
您需要使用 createdRow每当为表的主体创建 TR 元素时定义回调的属性。
$('#example').dataTable( {
"createdRow": function ( row, data, index ) {
$('td', row).eq(1).attr('id', 'td-' + index + '-1');
}
});
代码 $('td', row).eq(1)
用于使用从零开始的索引( 1
对于第二个单元格)选择表行中的第二个单元格。代码 attr('id', 'td-' + index + '-1')
将设置该单元格 id
属性为 td-0-1
第一行,td-1-1
对于第二行等,其中 index
是从零开始的行索引。
关于javascript - 在 DataTables 1.10.x 中设置 td id/name 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30537080/