动态添加行后,jquery DataTable 将 ID 添加到 TR 元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14596270/
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
jquery DataTable add ID to TR element after row is dynamically added
提问by Peter
I am using this code to init DataTable:
我正在使用此代码来初始化 DataTable:
$('#table-groups').dataTable( {
"bJQueryUI": true,
"sPaginationType": "full_numbers",
"bFilter": false,
"bInfo": false,
"aLengthMenu": [[10, 25, 50, -1], [10, 25, 50, "All"]],
"aoColumns": [
{ "sClass": "name" },
{ "sClass": "tools", "bSortable": false},
],
});
Now I am adding rows via server-side script like this:
现在我通过服务器端脚本添加行,如下所示:
$('#table-groups').dataTable().fnAddData( ["<strong>"+$_returnvalue.name+"</strong>","<div class=\"cell edit\"> Group ID is: "+$_returnvalue.entryid+" </div>"]);
And my question: is there a way to insert value of $_returnvalue.entryid
to be IDof the <tr>
?
我的问题:有没有办法插入值$_returnvalue.entryid
作为ID的 值<tr>
?
回答by Arun P Johny
You can use the fnCreatedRowcallback
您可以使用fnCreatedRow回调
You can do something like where aData[0]
has to be the id.
您可以执行诸如 whereaData[0]
必须是 id 之类的操作。
$('#example').dataTable( {
"fnCreatedRow": function( nRow, aData, iDataIndex ) {
$(nRow).attr('id', aData[0]);
}
});
Demo: Fiddle
演示:小提琴
回答by Rolwin Crasta
var MyUniqueID = "tr123"; // this is the uniqueId.
var rowIndex = $('#MyDataTable').dataTable().fnAddData([ "column1Data", "column2Data"]);
var row = $('#MyDataTable').dataTable().fnGetNodes(rowIndex);
$(row).attr('id', MyUniqueID);