【发布时间】:2021-12-02 07:41:54
【问题描述】:
如何根据在同一行的另一个单元格中找到的值设置变量的值(或者更具体地说,每行中的按钮属性)?此示例中的数据正在从 MVC 控制器传递到 Ajax 成功函数:
$.ajax({
type: "POST",
url: '@Url.Action("GetData", "Home")',
cache: false,
retrieve: true,
data: data,
contentType: "application/json; charset=utf-8",
dataType: "json",
defaultContent: true,
success: successFunc,
error: errorFunc
});
function successFunc(data) {
var firstName; //I'll need to set this variable with the name found in each row and assign that value to each Edit button attribute.
$("#tblMyTable").DataTable(
{
"info": true,
"data": data.list,
"responsive": true,
"autoWidth": false,
"bAutoWidth": false,
"retrieve": true,
columns: [
{ 'data': "ID", },
{ 'data': "First_Name" }, //This is the value I need. How can I set the value of the variable firstName using this data so that each button contains the attribute data-name with the value of the name column found in the current row?
{ 'data': "Last_Name" },
{ 'data': 'Edit', defaultContent: '<button type="button" data-name="' + firstName + '" href="javascript:;">Edit</button>' }
]
});
}
function errorFunc() {
}
}
【问题讨论】:
-
你可以绑定一个
click事件回调到它。点击后,收集具体细节。然后使用 AJAX 发送详细信息。 -
也可以看看这个帖子:datatables.net/forums/discussion/52485/…
-
参见How to use render function get data from another column 的示例,该示例展示了如何使用render function。这使您可以访问行中的每个字段。
-
另外,here 展示了如何使用渲染访问行数据的示例。
-
感谢大家为我指明正确的方向。我能够通过在 columnDefs 中添加渲染函数并通过访问 row.First_Name 参数来分配属性来找到解决方案 - 请参阅@devlincarnate 中的链接
标签: jquery ajax datatables