如何使用row.add(data).draw()在DataTable中添加行来设置TD的Id属性。 [英] How to set Id attribute of TDs while adding row in DataTable using row.add(data).draw().node();
问题描述
当我使用这个 API 考虑我只有一个TD。
How do I set the "id" attribute of one TD when I add a new row using this API of Datatables? Consider I have only one TD.
推荐答案
以下是来自对新添加的行执行操作的API的一个示例。 p>
Here is an example from the API that performs an operation on newly added rows.
var table = $('#example').DataTable();
table
.rows.add( [
new Pupil( 43 ),
new Pupil( 67 ),
new Pupil( 102 )
] )
.draw()
.nodes()
.to$()
.addClass( 'new' );
.addClass()
是一个jquery方法他们在这里使用新添加的行添加 class =new
。
.addClass()
is a jquery method that they use here to add class="new"
to the newly added rows.
因此,您可以在该行中找到所有< td>
,而不是向该行添加一个类使用 .find()
,然后使用 .each()
调用每个 < td>
使用 .attr()
修改其id属性。
So, instead of adding a class to the row, you could find all <td>
in that row using .find()
, then use .each()
to call a function on each <td>
to modify its id attribute using .attr()
.
var table = $('#example').DataTable();
var count = 0;
table
.rows.add( [
new Pupil( 43 ),
new Pupil( 67 ),
new Pupil( 102 )
] )
.draw()
.nodes()
.to$()
.find('td')
.each(function() {
$(this).attr('id', 'td' + (count++) );
});
请注意使用 count
确保分配的每个ID都是唯一的。
Notice the use of a count
variable to ensure that each id assigned is unique.
这篇关于如何使用row.add(data).draw()在DataTable中添加行来设置TD的Id属性。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!