javascript 数据表使用 fnAddData 或类似方法动态添加行并将类添加到特定列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14286528/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
datatables dynamically add row with fnAddData or similar and add a class to a specific column
提问by chris
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?
这适用于添加单行(如果有人知道如何使用类似的函数来添加多行而不运行一个循环的话)。但是我想在这种情况下有一个特定的列,第二列有一个特殊的类,有没有办法将一个类添加到正在添加的列中?
回答by Kevin
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.
我认为您可以通过控制列定义并通过 fnRender 分配类来实现这一点。定义列后,向 fnAddData 函数提供一些数据。
Here is a similar SO questions.. CLICK HEREthat I think you would find useful.
这是一个类似的 SO 问题..单击此处,我认为您会发现它很有用。
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>';
}
}
],
...
Via their api .. http://www.datatables.net/api... You could feed the table any number of rows via json
通过他们的 api .. http://www.datatables.net/api... 您可以通过 json 为表格提供任意数量的行
var json = eval("[" + response + "]");
oTable.fnAddData(json);
and let the datatable render any formatting itself dynamically
并让数据表动态呈现任何格式本身
回答by joeltine
For your first question, you can hook up to the "fnCreatedRow" callback, http://www.datatables.net/usage/callbacks. This will allow you to listen to row add events and manipulate them as necessary.
对于您的第一个问题,您可以连接到“fnCreatedRow”回调,http://www.datatables.net/usage/callbacks 。这将允许您侦听行添加事件并根据需要对其进行操作。
The "bonus" is that you can pass 2d-arrays to fnAddData to avoid looping
“奖励”是您可以将二维数组传递给 fnAddData 以避免循环