我使用以下代码在DataTables 1.10.2中使用table.row.add()
方法动态添加新行:
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();
同时尝试使用该索引进行以下操作,以添加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*()
函数的方法,请告诉我,谢谢!
table.row(i).data("id",id);
的操作,但是由于 DataTables 有一个名为data
的函数,所以无法这样做。你尝试过将table.row(i)
包装在 jQuery 函数中,然后调用 jQuery 的data
吗? - Blundering Philosophertable.row(i)
不是一个 jQuery 对象。你不能期望数据或属性函数能够与它一起工作。你需要先将它包装在 jQuery 函数中,然后才能使用其他函数$(table.row(i)).data("id", id);
。 - evolutionxbox