ony seven
ony seven

Reputation: 149

DataTable row.add with attr id

I have a DataTable

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

and i have function to add row

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

How to set an "id" attribute to this row?

Upvotes: 2

Views: 2924

Answers (1)

davidkonrad
davidkonrad

Reputation: 85528

row.add() return a dataTables API along the inserted row, so you can use API methods to set an id directly on the <tr> node :

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

Also remember that dataTables add a unique index to the DOM node called _DT_RowIndex :

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

would give you the unique index of the inserted record / row, and you can use that as a base for the id :

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

see a demo -> http://jsfiddle.net/4rqq82yr/

Upvotes: 5

Related Questions