SAPUI5已弃用me:Calendar返回页面后如何保留选中日期
Fixing Persistent Selected Date for Deprecated
me:Calendar I get it, relying on a deprecated component for that specific single-row layout can be tricky, but we can work around the date persistence issue with a few simple steps. Here's how to make sure the calendar remembers your selected date when navigating back:
Step 1: Save the Selected Date on Selection
First, whenever a user selects a date, store that date somewhere persistent (sessionStorage is perfect for this since it stays for the browser session). Update your onSelect handler:
function onSelect(oEvent) { const selectedDate = oEvent.getParameter("date"); // Store the date as an ISO string (easy to serialize/deserialize) sessionStorage.setItem("savedCalendarDate", selectedDate.toISOString()); // Proceed with your navigation logic this.getOwnerComponent().getRouter().navTo("YourTargetRoute"); }
Step 2: Restore the Date When the Page Loads
Next, when your calendar page loads (or right before it renders), fetch the saved date and set it back on the calendar component. Add this to your controller's onBeforeRendering or onInit method:
onBeforeRendering() { const savedDateString = sessionStorage.getItem("savedCalendarDate"); if (savedDateString) { const savedDate = new Date(savedDateString); const calendar = this.byId("WEEKLY_CALENDAR"); if (calendar) { // Set the calendar's displayed date to the saved one calendar.setDate(savedDate); // Also ensure the date stays selected calendar.setSelectedDate(savedDate); } } }
A Few Notes to Keep in Mind
- Since this is a deprecated component, double-check the method names: if
setDateorsetSelectedDatedon't work, look for similar methods likesetCurrentDatein the old component documentation. - If you're using a global state management solution (like a SAPUI5 JSON Model), you could store the date there instead of sessionStorage—this is better if you need to share the date across multiple components.
- Don't forget to clear the saved date if needed (e.g., if the user resets the calendar or logs out) with
sessionStorage.removeItem("savedCalendarDate").
内容的提问来源于stack exchange,提问作者Jünge alles
相关产品推荐
相关产品推荐

