如何通过单击 DataTable 同一行上的“编辑"按钮获取 Id 值 [英] How to get Id value by clicking Edit button on the same row of DataTable
问题描述
我使用 jQuery Datatable
来列出记录并添加一个 Action
按钮(编辑)以在模式对话框中编辑记录.如果我选择一行,我可以获得行 id 值并在模态对话框中打开相关记录.但是,如果我直接点击Edit
按钮,我无法获取相关记录的Id值(在同一行),因为点击Edit
按钮时没有先选择它.我想要做的是:我想获取我单击编辑"按钮所在行的 Id 值.是否可以?如果没有,我可以在单击 Edit
按钮时以编程方式选择悬停的行吗?(如果之前的场景是可能的,我会更喜欢它).有什么想法吗?
I use jQuery Datatable
for listing records and add an Action
button (Edit) for editing the record on a modal dialog. If I select a row I can get the row id value and open the related record on modal dialog. However, if I click the Edit
button directly, I cannot get the Id value of the related record (on the same row) because it is not selected first when clicking Edit
button. What I want to do is that: I want to get the Id value of the row on which I click the Edit button. Is it possible? If not, can I select the hovered row programmatically when I click the Edit
button? (If the prior scenario is possible I would prefer it). Any idea?
function openModal() {
var table = $('#dtbListAccount').DataTable();
var oRow = $('this').parents('tr')[0];
var oData = table.fnGetData(oRow);
//code omitted for brevity
};
推荐答案
您可以使用此代码来实现这一点.
You can use this code to achieve this.
var table;
$(document).ready( function () {
table = $('#example').DataTable();
} );
$('body').on('click', '#btnEdit', function(){
//to get currently clicked row object
var row = $(this).parents('tr')[0];
//for row data
console.log( table.row( row ).data() );
});
它将以字符串数组的形式返回行数据.
It will return row data as a string array.
使用浏览器控制台查看结果.
Use the browser console to see the results.
这篇关于如何通过单击 DataTable 同一行上的“编辑"按钮获取 Id 值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!