我有一个DataTable ajax,它将值传递给控制器。有关我的简化代码,请参阅下面的代码段。
$(function() {
$("#tableDiv").hide();
$("#submitDateFilters").on("click", function() {
displayData();
$("#tableDiv").show();
document.getElementById("dateFilters").reset();
});
});
////////////////////
// Table Header Contents
////////////////////
var tblReport = {};
var table;
var tableCont = {
"db_columns": [
"TYPE", "USER_ID", "USER_NAME", "DATE_ACCESS", "SESSION_ID", "PROJECT"
]
};
var obj = new Object();
////////////////////
// Create Table
////////////////////
function createTbl(wrapper) {
wrapper.empty();
wrapper.append(
$("<table/>").append($("<thead/>"), $("<tfoot/>"))
);
// row header
var row = $("<tr/>");
$.each(tableCont["db_columns"], function(i, val) {
row.append($("<th/>").text(val.replace(/\_/g, " ")));
});
$("table", wrapper).find("thead, tfoot").append(row);
return $("table", wrapper);
}
////////////////////
// Initialize Datatables
////////////////////
function displayData() {
var wrapper = $(".tbl-wrapper");
var dataObj = new Object();
dataObj.cols = tableCont["db_columns"];
var sDate = $("#startDate").val();
var eDate = $("#endDate").val();
if ($.isEmptyObject(tblReport)) {
//** create table
tblReport = createTbl(wrapper);
//** assign datatable columns
var columns = [];
$.each(tableCont["db_columns"], function(k, val) {
columns.push({
"data": val
});
});
//** initialize datatable
table = tblReport.DataTable({
"order": [
[3, "desc"]
],
"lengthMenu": [
[20, 50, 100, 200],
[20, 50, 100, 200]
],
"serverSide": true,
"processing": true,
"deferRender": true,
"destroy": true,
"ajax": {
"url": "getTableData",
"type": "POST",
"data": {
jsonData: JSON.stringify(dataObj),
startDate: sDate,
endDate: eDate
},
"cache": false
},
"columns": columns,
"columnDefs": [{
"data": undefined,
"defaultContent": "",
"targets": "_all"
}],
"drawCallback": function() {
//** re-adjust filters WIDTH
},
"initComplete": function() {
//** initialize COLUMN FILTERS
}
});
//** reload page on DATATABLE ERROR
$.fn.dataTable.ext.errMode = function(error) {
alert(error.toString());
//window.location.reload(true);
};
} else {
table.ajax.reload();
}
}
$("#startDate").datepicker({
maxDate: '0',
constrainInput: true,
changeYear: true,
changeMonth: true,
showOtherMonths: true,
selectOtherMonths: true,
onSelect: function(selectedDate) {
$("#endDate").datepicker("option", "minDate", selectedDate);
}
});
$("#endDate").datepicker({
maxDate: '0',
constrainInput: true,
changeYear: true,
changeMonth: true,
showOtherMonths: true,
selectOtherMonths: true,
onSelect: function(selectedDate) {
$("#startDate").datepicker("option", "maxDate", selectedDate);
}
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.11/jquery.min.js"></script>
<div class="report-wrapper" align="center">
<div style="margin-bottom:5px;" align="center">
<form method="POST" action="getTableData" enctype="multipart/form-data" id="dateFilters">
<table>
<tr>
<td class="txt-bold"> FILTER DATE FROM: </td>
<td><input type="text" name="startDate" id="startDate" /></td>
<td class="txt-bold"> TO: </td>
<td style="padding-left:5px;"><input type="text" name="endDate" id="endDate" /></td>
<td><button type="button" id="submitDateFilters">Search</button></td>
</tr>
</table>
</form>
</div>
<div class="datatbl" style="width:1115px;" id="tableDiv"></div>
</div>
&#13;
单击“搜索”按钮,将调用函数displayData(),并且ajax调用将发送请求。这适用于第一次调用,但在以下按钮单击时,ajax会发送startDate和endDate的第一个调用值。
ajax /我的代码有什么问题,如何解决?
修改
我刚刚发现这是由我的脚本的ajax.reload()
引起的。我已将DataTable代码添加到代码段。
答案 0 :(得分:0)
对ajax.data
选项使用函数而不是Object。并在调用函数时从startDate和endDate控件中检索数据。
如果对ajax.data
选项使用object,则在初始化时只会对代码进行一次评估。如果对ajax.data
选项使用函数,则每次加载或重新加载表时都会调用它。
例如:
"ajax": {
"url": "getTableData",
"type": "POST",
"data": function ( d ) {
d.jsonData = JSON.stringify(dataObj);
d.startDate = $("#startDate").val();
d.endDate = $("#endDate").val();
}
},