我已经编写了一个代码来将动态表显示到 DataTable 中。
<table id="tag" class="display table table-striped table-bordered" cellspacing="0" width="100%">
<thead>
<tr>
@foreach (var item in Model.HeaderModelList.Select((value, i) => new { i, value }))
{
<th id=@(item.i+1)>
@item.value.Category
<span class="filterExcel">
<select id="tag@(item.value.PatientTagCategoryId)" name="tag" asp-items="@item.value.HeaderOptions" multiple class="drp-tagMulti-Select">
</select>
</span>
</th>
}
<th id="@(Model.HeaderModelList.Count()+1)">Simulation Name
</th>
<th id="@(Model.HeaderModelList.Count()+2)">
Patient Name
</th>
</tr>
</thead>
</table>
在更改下拉菜单时,我调用了一个调用 reload jquery 的事件。
$(document).ready(function () {
var id = "";
var newval = "";
$('.drp-tagMulti-Select').on('change', function () {
var valid = this.id;
var val = $('#' + valid).val();
if (valid) {
newval = val;
id = valid;
console.log("id" + id);
console.log("newval" + newval);
table.ajax.reload();
}
});
var table = $("#tag").DataTable({
"aLengthMenu": [[10, 25, 50, 75, -1], [10, 25, 50, 75, "All"]],
"iDisplayLength": 10,
"serverSide": true,
"processing": true,
"stateSave": true,
"search": true,
"ajax": {
'type': 'POST',
'dataType': 'json',
'data': {
TagId: id,
Values: newval
},
'url': 'GetFilteredPatientTags',
'dataSrc': function (d) {
var values = [];
for (var i = 0; i < d.data.length; i++) {
var result = Object.values(d.data[i]);
values.push(result);
}
return values;
}
}
});
$(".filterExcel").click(function (e) {
e.stopPropagation();
})
$('.drp-tagMulti-Select').multipleSelect({
placeholder: 'Select specific course',
ellipsis: true,
filter: true,
filterAcceptOnEnter: true,
animate: 'fade',
width: 20
})
$('.ms-drop').width('fit-content')
});
现在,每当我更改下拉菜单时,都会触发事件,id
并且newval
控制台中会正确显示和 的值
console.log("id" + id);
console.log("newval" + newval);
然后我重新加载数据表,但的值id
和newval
不正确地传递在AJAX,该值被发送为空
如果我改变的初始值id
,并newval
为“A”和“B”
var id = "a";
var newval = "b";
那么通过ajax的id
和newval
传递的值总是“a”和“b”,我需要在ajax中传递控制台中显示的值。
我怎样才能解决这个问题?
要解决它,您需要将一个函数传递给ajax.data:
"ajax": {
...
'data': function(d) {
d.TagId = id;
d.Values = newval;
},
...
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句