我有两个DatePicker输入字段,称为“startDate”& “结束日期”。
选择“startDate”时,应将“enddate”的最大值设置为"startDate" + 180 days
。它还应将“endDate”的最小值设置为"startDate"
查看如下:
var oTableTopFilterForm = new sap.ui.layout.form.Form({
editable: true,
layout: new sap.ui.layout.form.ResponsiveLayout(),
formContainers:[
new sap.ui.layout.form.FormContainer({
formElements:[
new sap.ui.layout.form.FormElement({
label: new sap.m.Label({
text:"Start Date"
}),
fields:[
new sap.m.DatePicker("startDate",{
forceSelection: false,
enabled:false,
valueFormat: "yyyy-MM-dd",
placeholder:"Choose Start Date ... ",
change:[oController.onStartDateSelect]
})
]
}),
new sap.ui.layout.form.FormElement({
label: new sap.m.Label({
text:"EndDate"
}),
fields:[
new sap.m.DatePicker("endDate",{
forceSelection: false,
enabled:false,
placeholder:"Choose End Date ... ",
change:[]
})
]
}),
]
})
]
}); // oTableTopFilterForm
控制器中的onStartDateSelect函数如下:
var startdate = sap.ui.getCore().byId("startDate").getDateValue();
var enddate = sap.ui.getCore().byId("endDate");
enddate.setEnabled(true);
var tempDate = new Date(startdate);
var enddatemax = new Date(tempDate);
enddatemax.setDate(startdate.getDate() + 180);
var currentDate = new Date();
if(enddatemax > currentDate)
{
// This condition works fine
alert(enddatemax);
alert("End Date is bigger than Current Date");
enddate.setMaxDate(currentDate);
enddate.setMinDate(startdate);
}
else
{
alert(enddatemax);
alert("End Date is smaller than Current Date");
enddate.setMinDate(startdate);
enddate.setMaxDate(enddatemax);
alert(enddate.getMinDate());
alert(enddate.getMaxDate());
}
}
直到现在都没有错误。最后两个警报为endDate提供了正确的MinDate和MaxDate。
但是现在当我尝试选择endDate时,它会出现以下错误:
Uncaught Error: Date must not be in valid range (minDate and maxDate); Element sap.ui.unified.Calendar#endDate-cal
at f.n (Calendar.js:6)
at f.c.focusDate (Calendar.js:6)
at f.a._fillDateRange (DatePicker.js:6)
at f._ (DatePicker.js:6)
at f.b (DatePicker.js:6)
at f.a.onclick (DatePicker.js:6)
at f.a._handleEvent (sap-ui-core.js:954)
at constructor.U._handleEvent (sap-ui-core.js:1178)
at HTMLDivElement.p (sap-ui-core.js:115)
at HTMLDivElement.dispatch (sap-ui-core.js:126)
当正确设置最小和最大日期时,不确定为什么DatePicker会给我一个错误。
有什么想法吗?