数据表使用fnAddData或类似的动态添加行,并将类添加到特定列 [英] datatables dynamically add row with fnAddData or similar and add a class to a specific column
问题描述
好的,我试图使用数据表将动态的行添加到已经呈现的表中。到目前为止,我所拥有的是
Ok I am trying to dynamically add new rows to a already rendered table using datatables. Thus far what I have is
oTable.fnAddData(["D:\Exlab", '[<a href="#" class="datasource_row_edit" data-idr="reference">Edit</a>] [<a href="#" class="datasource_row_delete" data-idr="reference">Delete</a>]']);
这可以用于添加单个行(如果有人知道如何使用类似的功能添加多个行没有运行一个循环,将是奖金)。然而,我想在这种情况下有一个特定的列,第二列有一个特殊的类,是否有一个方法来添加一个类,被添加在一个列中?
Which this works for adding a single row (if anyone knows how to use a similar function to add multiple rows without running a loop that would be bonus). However I want to have a specific column in this case the second column have a special class, is there a means of adding a class to a column thats being added on the fly?
推荐答案
我认为您可以通过控制列定义并通过fnRender分配类来完成此任务。定义列后,将fnAddData函数的一些数据提供给。
I think you could accomplish this by controlling the column definitions and assigning the class via fnRender. After your columns are defined, feed the fnAddData function some data.
这里有一个类似的SO问题.. 点击这里,我认为你会发现有用的。
Here is a similar SO questions.. CLICK HERE that I think you would find useful.
在你的情况下,我认为列定义看起来像这样
In your case, I think that the column definitions would look something like this
...
"aoColumns": [
{
"sClass": "datasource_row_edit",
"fnRender": function( oObj ) {
return '<a href="#" data-idr="reference">Edit</a>';
}
},
{
"sClass": "datasource_row_delete",
"fnRender": function( oObj ) {
return '<a href="#" data-idr="reference">Delete</a>';
}
}
],
...
通过他们的api .. http://www.datatables.net/api ...你可以通过json将表格提供给任意数量的行
Via their api .. http://www.datatables.net/api ... You could feed the table any number of rows via json
var json = eval("[" + response + "]");
oTable.fnAddData(json);
,让datatable自动呈现任何格式化本身
and let the datatable render any formatting itself dynamically
这篇关于数据表使用fnAddData或类似的动态添加行,并将类添加到特定列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!