简体   繁体   中英

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?

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/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM