on 2025 Apr 08 3:42 PM
Problem Description: I am facing an issue where I am unable to properly bind DatePicker controls in an SAP UI5 fragment and use them for filtering an OData model. Specifically, when I try to retrieve values from the DatePicker controls and apply filters to the OData model, I encounter the error sap.m.MessageToast.show("DatePicker controls are not available."); This error suggests that the DatePicker controls are not accessible, but they are part of the opened fragment.
Scenario: I have a fragment that includes two DatePicker controls (DPFrom and DPTo), and I want to use them for filtering records based on a date range (ALERTE_CRE_DATE) from an OData model. The error occurs when trying to access the DatePicker values in the controller. you should know that the fragment is opend
Here’s a part of the relevant code I’m working with:
<core:FragmentDefinition xmlns="sap.m" xmlns:core="sap.ui.core">
<Dialog
id="helloWorldDialog"
title="Date Selection"
class="sapUiResponsivePadding"
resizable="true"
stretchOnPhone="true"
afterClose="onDialogClose">
<Panel
id="datePanel"
width="auto">
<!-- 'From' DatePicker -->
<Label text="From:" labelFor="DPFrom"/>
<DatePicker
id="DPFrom"
placeholder="Enter From Date..."
change="handleChangeFrom"
class="sapUiSmallMarginBottom"/>
<!-- 'To' DatePicker -->
<Label text="To:" labelFor="DPTo"/>
<DatePicker
id="DPTo"
placeholder="Enter To Date..."
change="handleChangeTo"
class="sapUiSmallMarginBottom"/>
<FlexBox
height="30px"
alignItems="End"
justifyContent="End">
<items>
<Button text="Select" press="onSelectPress" type="Emphasized" class="sapUiSmallMarginEnd" />
<Button text="Cancel" press="onCancelPress" class="sapUiTinyMarginEnd" />
</items>
</FlexBox>
</Panel>
</Dialog>
</core:FragmentDefinition>onhandleDate: function(oEvent) {
var that = this;
if (!this._valueHelpDialogDate) {
this._valueHelpDialogDate = sap.ui.xmlfragment("DateFilter", "com.faurecia.stockMonitorCatalog.view.ValueHelpDialogDate", this);
this.getView().addDependent(this._valueHelpDialogDate); // Ensures the fragment is added as a dependent
}
this._valueHelpDialogDate.open();
},
onSelectPress: function(oEvent) {
var oFromDate = this.byId("DPFrom");
var oToDate = this.byId("DPTo");
// Check if the DatePicker controls are available
if (!oFromDate || !oToDate) {
sap.m.MessageToast.show("DatePicker controls are not available.");
return;
}
var fromDateValue = oFromDate.getDateValue();
var toDateValue = oToDate.getDateValue();
if (fromDateValue && toDateValue) {
// Create filters for the OData model
var aFilters = [
new sap.ui.model.Filter("ALERTE_CRE_DATE", sap.ui.model.FilterOperator.GE, fromDateValue),
new sap.ui.model.Filter("ALERTE_CRE_DATE", sap.ui.model.FilterOperator.LE, toDateValue)
];
// Apply filters to the OData model and read data
var oModel = this.getView().getModel("listModel");
oModel.read("/StockDataSet", {
filters: aFilters,
success: function(oData) {
// Update the model with the filtered data
var oFilteredModel = new sap.ui.model.json.JSONModel(oData.results);
this.getView().setModel(oFilteredModel, "filteredModel");
// Close the DatePicker dialog
this._valueHelpDialogDate.close();
}.bind(this), // Bind the correct context
error: function() {
sap.m.MessageToast.show("Error occurred while fetching data.");
}
});
} else {
sap.m.MessageToast.show("Please select both 'From' and 'To' dates.");
}
}
Request clarification before answering.
Hi @rimene99 ,
The problem lies in how you are trying to access the DatePickers in the controller, in the lines below:
var oFromDate = this.byId("DPFrom");
var oToDate = this.byId("DPTo");this.byId("") does not work because it can only access controls inside the view and not inside the fragment.
You can replace the above section of your code with the below code:
var oFromDate = sap.ui.core.Fragment.byId("DateFilter", "DPFrom");
var oToDate = sap.ui.core.Fragment.byId("DateFilter", "DPTo");Please let me know if this helped solve your problem!
Also please have a look at the latest standards to load fragments in you controller here!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
| User | Count |
|---|---|
| 6 | |
| 5 | |
| 4 | |
| 3 | |
| 3 | |
| 3 | |
| 3 | |
| 2 | |
| 2 | |
| 2 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.