我使用以下代码使用 table.row.add()
方法在 DataTables 1.10.2 上动态添加新行:
table.row.add([
'',
name,
target_l,
details,
panel.html()
]).draw();
我制作了这个标记:
<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>
我想做的是添加一个data-id(和其他数据)属性到新添加的tr标签(在行插入时或之后)并制作它是这样的:
<tr data-id="59" role="row" class="odd">
我已经设法使用代码获取新添加行的索引,它返回最后一行索引:
var i = table.row.add([
'',
name,
target_l,
details,
panel.html()
]).index();
并且还尝试执行以下操作以使用 that 索引添加 data-id 属性:
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.
我是 DataTables 的新手,已经滚动了它的源代码,将评论标红。虽然不善于理解它以_fn*()
开头的功能。如果有任何其他方式不依赖于这些 _fn*()
函数,谢谢!
最佳答案
您可以使用 rows().nodes() API函数,见下:
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);
关于javascript - 将数据属性放在 DataTables 1.10 的行上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26749496/