我正在使用 ASP.NET Core 6 MVC C#。我有一张这样的桌子:
<div class="card-body">
<table class="table display table-bordered" id="DATATABLE"></table>
</div>
我有一个带有 StudID (int - PrimaryID)、Name (string)、Class (int) 属性的 Student 类。表格显示如下。
在上图中,Class 值 9 是一个超链接,100 是该行的 PrimaryID。
我正在寻找一种在单击 9 时获取 PrimaryID 的方法。
我尝试过的如下所示。
$("#DATATABLE").DataTable({
serverSide: true,
filter: true,
searchDelay: 1000,
scrollY: StaticData.TABLE_HEIGHT + 'px',
lengthMenu: StaticData.TABLE_PAGE_SIZE,
scrollCollapse: true,
ajax: {
url: '/STUD_MANAGEMENT/LoadStudents',
type: 'GET',
datatype: 'json',
headers: { 'RequestVerificationToken': 'your json token' },
dataSrc: (json) => {
json = json.data;
return json;
}
},
columnDefs: [{ className: "dt-center", targets: [3], width: '2%', }],
columns: [
{ data: 'STUD_ID', title: 'Stud ID', autoWidth: false, visible : false },
{ data: 'NAME', title: 'Name', autoWidth: true },
{ data: "CLASS", title: 'Class', autoWidth: true,
"render": function(data, type, row, meta){
if(type === 'display')
{
data = '<a href="' + data + '" onclick = "clickLink()">' + data + '</a>';
}
return data;
}
}
],
});
function clickLink() {
alert('studid is ??? ');
}
您必须将更多信息发送至本栏目
render
(通过data
)。
尝试下面的代码:
$("#DATATABLE").DataTable({
serverSide: true,
filter: true,
searchDelay: 1000,
scrollY: StaticData.TABLE_HEIGHT + 'px',
lengthMenu: StaticData.TABLE_PAGE_SIZE,
scrollCollapse: true,
ajax: {
url: '/STUD_MANAGEMENT/LoadStudents',
type: 'GET',
datatype: 'json',
headers: { 'RequestVerificationToken': 'your json token' },
dataSrc: (json) => {
json = json.data;
return json;
}
},
columnDefs: [{ className: "dt-center", targets: [3], width: '2%', }],
columns: [
{ data: 'STUD_ID', title: 'Stud ID', autoWidth: false, visible : false },
{ data: 'NAME', title: 'Name', autoWidth: true },
{ data: null, title: 'Class', autoWidth: true,
"render": function(data, type, row, meta){
if(type === 'display')
{
data = '<a href="' + data.CLASS + '" onclick = "clickLink('+data.STUD_ID+')">' + data.CLASS + '</a>';
}
return data;
}
}
],
});
function clickLink(id) {
alert('studid is '+id);
}