jQuery 数据表将 id 添加到添加的行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21114555/
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 dataTables add id to added row
提问by Sebastien
Is it possible to add ID to last added row with jQuery DataTables (e.g., <tr id="myid">...</tr>
) ?
是否可以使用 jQuery DataTables(例如,<tr id="myid">...</tr>
)将 ID 添加到最后添加的行?
$('#example').dataTable().fnAddData( [
"col_value_1",
"col_value_2",
"col_value_3",
"col_value_4" ] );
(Add id to this new row)
(将 id 添加到这个新行)
回答by dcodesmith
Use the fnCreatedRow/createdRowCallback. It is best to set the id attribute of the table row on creation of the row. Use what the API has provided and you won't need to hack it or have messy code
使用fnCreatedRow/createdRow回调。最好在创建行时设置表行的 id 属性。使用 API 提供的内容,您无需破解它或编写凌乱的代码
This function is called when a TR element is created (and all TD child elements have been inserted), or registered if using a DOM source, allowing manipulation of the TR element (adding classes etc).
该函数在创建 TR 元素时调用(并且所有 TD 子元素都已插入),或者在使用 DOM 源时注册,允许操作 TR 元素(添加类等)。
//initialiase dataTable and set config options
var table = $('#example').dataTable({
....
'fnCreatedRow': function (nRow, aData, iDataIndex) {
$(nRow).attr('id', 'my' + iDataIndex); // or whatever you choose to set as the id
},
....
});
// add data to table post-initialisation
table.fnAddData([
'col_value_1',
'col_value_2',
'col_value_3',
'col_value_4'
]);
回答by Rolwin Crasta
This worked for me
这对我有用
var MyUniqueID = "tr123"; // this is the uniqueId.
var rowIndex = $('#MyDataTable').dataTable().fnAddData([ "column1Data", "column2Data"]);
var row = $('#MyDataTable').dataTable().fnGetNodes(rowIndex);
$(row).attr('id', MyUniqueID);
回答by RogerMKE
If you use row.add() it is easy to set the id:
如果您使用 row.add() 很容易设置 id:
var table = $('#example').DataTable();
var rowNode = table.row.add([
"col_value_1",
"col_value_2",
"col_value_3",
"col_value_4"
]).node();
$(rowNode).attr("id", myid);
The .node() returns the element of the newly added row.
.node() 返回新添加行的元素。
回答by Sammie
回答by YakovGdl35
This worked for me
这对我有用
var rowNode=$('#MyTable').DataTable().row.add([1,2,3]).draw( false );
rowNode.id='myId';
rowNode.id='myId';
rowNode.id='myId';
回答by Pragnesh Khalas
Hope below code will help you
希望下面的代码可以帮助你
var rowid = $('#example').dataTable().fnAddData( [
"col_value_1",
"col_value_2",
"col_value_3",
"col_value_4" ] );
var theNode = $('#example').dataTable().fnSettings().aoData[rowid[0]].nTr;
theNode.setAttribute('id','your value');