javascript - jquery dataTable Ajax调用中未传递参数
问题描述
我编写了一个代码来将动态表显示到 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);
然后我重新加载数据表,但是ajax 的值id
并newval
没有正确传递,值作为 null 发送
id
如果我将and的初始值更改newval
为“a”和“b”
var id = "a";
var newval = "b";
那么通过ajax的值总是“a”和“b”,我需要在ajax中传递控制台中显示的值id
。newval
我怎样才能解决这个问题?
解决方案
要解决它,您需要将一个函数传递给ajax.data:
"ajax": {
...
'data': function(d) {
d.TagId = id;
d.Values = newval;
},
...
推荐阅读
- mysql - 在 Laravel 中以一对多关系显示要查看的数据
- c# - 我有一个例外情况:将项目添加到组合框时出现 System.NullReferenceException
- powershell - 比较两个文件的内容并输出内容的差异以及行号
- reporting-services - 精确地在 SSRS 报告中移动像文本框这样的报告项?
- jenkins - 压缩詹金斯工作区文件夹中的所有文件
- mysql - MySQL order by before group by 不起作用
- java - 如何记录没有任何上下文的不安全请求?
- java - 在本地重现和解决 Android java.lang.unsatisfiedLinkError
- mysql - mysql 不适用于 AWS linux 实例?
- ios - 以编程方式添加“间距到最近的邻居”约束