带有attr id的DataTable row.add

时间:2017-04-17 02:06:24

标签: javascript datatables

我有一个DataTable

var tab2=$('#datatable2').DataTable({
        "searching": false,
        "bPaginate": false,
        "bLengthChange": false,
        "bFilter": true,
        "bInfo": false,
        "bAutoWidth": false,
        "columnDefs": [
           { className: "kiriya", "targets": [ 4 ] }
         ]
        });

我有添加行的功能

tab2.row.add( [
                 tipe,
                 nama,
                 harga,
                 vol,
                 tot
              ] ).draw( false );

如何为此行设置“id”属性?

1 个答案:

答案 0 :(得分:2)

row.add()沿插入的行返回dataTables API,因此您可以使用API​​方法直接在id节点上设置<tr>

var row = table.row.add(['a','b','c','d','e','f']).draw();
row.nodes().to$().attr('id', 'someId');

还要记住,dataTables为名为_DT_RowIndex的DOM节点添加唯一索引:

console.log(row.node()._DT_RowIndex)

会为您提供插入记录/行的唯一索引,您可以将其用作id的基础:

row.nodes().to$().attr('id', 'tr'+row.node()._DT_RowIndex);

查看演示 - &gt;的 http://jsfiddle.net/4rqq82yr/