您可以使用导航事件来执行此操作。首先,在视图中声明datepicker(下面是xml视图的示例),并为navigate事件提供回调。
<DatePicker id="some_date" navigate="onClickDate"/>
然后,在控制器中为此视图声明回调函数。
onClickDate: function(){
//do something
}
希望这可以帮助。
您可以使用navigate
事件,但每当用户在日历中导航时,它也会再次触发。
从技术上讲,您可以使用值帮助图标(press
)的oDatePicker._getValueHelpIcon().attachPress()
事件,但这可能会令人不悦,因为它使用控件的私有方法。
您可以在显示日期时使用“isOpen()”或使用“getValue()”获取日历上的最后一个选定值并由change事件返回,其他方法可在此处获取>> DatePicker Methods
//DatePicker View.js
var datePicker = new DatePicker(this.createId("calendarSelect"), {
width: "80%",
maxDate: new Date(),
valueFormat: "yyyy-MM-dd",
displayFormat: "long",
change: function() {
oController.someFunction();
},
value: new Date().toDateString("en-GB")
});
//DatePicker Controller.js
someFunction: function() {
console.log(this.getView().byId("calendarSelect").isOpen()); // returns true or false when the calendar is open
console.log(this.getView().byId("calendarSelect").getValue()); //to get value selected
}
从1.62 commit开始,navigate
event提供了一个额外的标志参数"afterPopupOpened"
,只有当选择器刚刚打开时才设置为true
。否则,当用户在日历中导航时,它就像往常一样是false
。
这是一个演示:
sap.ui.getCore().attachInit(() => sap.ui.require([
"sap/m/DatePicker",
"sap/m/MessageToast",
], (DP, MsgToast) => new DP({
placeholder: `Press F4 or click on the icon -->`,
navigate: e => e.getParameter("afterPopupOpened") && MsgToast.show("Calendar opened!"),
}).setWidth("16rem").placeAt("content")));
<script id="sap-ui-bootstrap"
src="https://openui5nightly.hana.ondemand.com/resources/sap-ui-core.js"
data-sap-ui-libs="sap.ui.core, sap.m, sap.ui.unified"
data-sap-ui-async="true"
data-sap-ui-theme="sap_belize"
data-sap-ui-compatversion="edge"
data-sap-ui-xx-waitfortheme="true"
></script>
<body id="content" class="sapUiBody sapUiSizeCompact" style="height: 100%;"></body>