参数未传入 jquery dataTable Ajax 调用
Parameter Not Passing in jquery dataTable Ajax Call
我写了一个代码来显示动态 table 到数据 Table。
<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>
在更改下拉列表时,我调用了一个调用重新加载的事件 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);
然后我重新加载数据-Table,但是 id
和 newval
的值在 ajax 中没有正确传递,值被发送为空
如果我将 id
和 newval
的初始值更改为“a”和“b”
var id = "a";
var newval = "b";
那么id
和newval
通过ajax的值总是“a”和“b”,我需要将控制台中显示的值传递给ajax.
我该如何解决这个问题?
要解决它,你需要传递一个函数给ajax.data:
"ajax": {
...
'data': function(d) {
d.TagId = id;
d.Values = newval;
},
...
我写了一个代码来显示动态 table 到数据 Table。
<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>
在更改下拉列表时,我调用了一个调用重新加载的事件 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);
然后我重新加载数据-Table,但是 id
和 newval
的值在 ajax 中没有正确传递,值被发送为空
如果我将 id
和 newval
的初始值更改为“a”和“b”
var id = "a";
var newval = "b";
那么id
和newval
通过ajax的值总是“a”和“b”,我需要将控制台中显示的值传递给ajax.
我该如何解决这个问题?
要解决它,你需要传递一个函数给ajax.data:
"ajax": {
...
'data': function(d) {
d.TagId = id;
d.Values = newval;
},
...