Reputation: 57
I have written a code to display the dynamic table into 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>
On changing the drop-down I have called an event that calls 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')
});
Now whenever I change the dropdown the event is triggered and the values of id
and newval
is displayed correctly in the console
console.log("id" + id);
console.log("newval" + newval);
and then I reload the Data-Table, but the value of id
and newval
is not correctly pass in the ajax, the value is send as null
If I change the initial value of id
and newval
as "a" and "b"
var id = "a";
var newval = "b";
then the value of id
and newval
passing through ajax is always "a" and "b", I need to pass the value that is displayed in the console in the ajax.
How Can I fix this?
Upvotes: 0
Views: 1891
Reputation: 11975
To solve it, you need to pass a function to ajax.data:
"ajax": {
...
'data': function(d) {
d.TagId = id;
d.Values = newval;
},
...
Upvotes: 1