使用jQuery Datatables在Ajax调用中从表单元格中找到的值设置变量的值



如何从同一行的另一个单元格中找到的值设置变量的值(或者更具体地说-每行中的按钮属性)?本例中的数据从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() {
}
}

在columnDefs中使用渲染函数对我来说很有效。columnDefs应该遵循columns选项:

"columnDefs": [
{
"targets": [3],
"render": function (data, type, row, meta) {
{
return '<button type="button" data-name="' + row.First_Name + '" href="javascript:;">Edit</button>'
}
}
}
]

最新更新