Put data attribute on add string in DataTables 1.10

I am dynamically adding a new row to DataTables 1.10.2 using the table.row.add() method using this code:

 table.row.add([ '', name, target_l, details, panel.html() ]).draw(); 

I made this allowance:

 <tr role="row" class="odd"> <th>1 .</th> <td class="sorting_1">ID Fee</td> <td>All students</td> <td></td> <td> <button class="btn btn-null btn-xs" onclick="_remove(59, 'ID Fee')"> <span class="fui-cross"></span> </button> <button class="btn btn-null btn-xs" onclick="_edit(59, 'ID Fee', '', '')"> <span class="icon-pencil"></span> </button> </td> </tr> 

What I want to do is add an attribute (and other data) to the newly added tr tag (insert or add a row) and make it something like this:

 <tr data-id="59" role="row" class="odd"> 

I managed to get the index of the added row using code and it returns the index of the last row:

 var i = table.row.add([ '', name, target_l, details, panel.html() ]).index(); 

And also tried the following to add the data-id attribute using this index:

 var id = $("#department_id").val(); table.row(i).attr("data-id", id); // table.row(i).data("id", id); // I wanted to try this but there is also a method called data() already in // DataTables so it will not work like in JQuery. 

I am new to DataTables and have already scrolled its source code, red comments. Although this does not well understand its functions, starting with _fn*() . If there is any other way without relying on these _fn*() functions, thanks!

+8
javascript jquery html jquery-datatables
source share
1 answer

You can use rows (). nodes () API, see below:

 var i = table.row.add([ '', name, target_l, details, panel.html() ]).index(); var id = $("#department_id").val(); table.rows(i).nodes().to$().attr("data-id", id); 
+9
source share

All Articles