如何在TR和TD中添加属性?

Dar*_*ber 26 datatables

我想使用数据数据表添加行,我可以这样做

var table = $('#mytable').DataTable();
table.add.row(['first column', 'second column', 'three column', 'etc']);
Run Code Online (Sandbox Code Playgroud)

我需要的是这样的东西(TR和TD标签中的一些属性)

<tr id="someID">
<td>first column</td>
<td>second column</td>
<td>three column</td>
<td id="otherID">etc</td>
</tr>
Run Code Online (Sandbox Code Playgroud)

我如何用数据表做到这一点?

Gyr*_*com 70

使用createdRowcolumns.createdCell选项来定义一个回调函数时将被调用TR,并TD创建元素.

$('#example').dataTable( {
  'createdRow': function( row, data, dataIndex ) {
      $(row).attr('id', 'someID');
  },
  'columnDefs': [
     {
        'targets': 3,
        'createdCell':  function (td, cellData, rowData, row, col) {
           $(td).attr('id', 'otherID'); 
        }
     }
  ]
});
Run Code Online (Sandbox Code Playgroud)

有关代码和演示,请参阅此示例.