Accessing controls inside a fragment depends on how your fragment was created. Here is a list of scenarios with the respective API to use to get the control reference.
Given:
this
as a reference to the current controller instance
<MyControl id="controlId"/>
in the fragment definition
Fragment
required from sap/ui/core/Fragment
Element
required from sap/ui/core/Element
API to choose
... if the fragment was created with the view ID (recommended) either directly or indirectly:
-
// Recommended; API since 1.93
this.loadFragment({ name: "..." }); // id: view ID given by default, API since 1.93
-
<!-- Recommended; in the view embedding the fragment declaratively -->
<core:Fragment fragmentName="..." type="XML"/><!-- id = view ID given by default -->
-
Fragment.load({ // API since 1.58
id: this.getView().getId(),
name: "...",
controller: this,
});
sap.ui.xmlfragment(this.getView().getId(), "...", this); // Deprecated
Resulting global ID: "componentId---viewId--controlId"
*
πthis.byId(Fragment.createId("fragmentId", "controlId"));
... if a fragment ID was given with the view ID combined:
-
this.loadFragment({ id: this.createId("fragmentId"), name: "..." });
-
<core:Fragment id="fragmentId" fragmentName="..." type="XML"/>
-
Fragment.load({
id: this.createId("fragmentId"),
name: "...",
controller: this,
});
sap.ui.xmlfragment(this.createId("fragmentId"), "...", this); // Deprecated
Resulting global ID: "componentId---viewId--fragmentId--controlId"
*
πFragment.byId("fragmentId", "controlId");
... if only the fragment ID was given without combining with the view ID:
-
this.loadFragment({
id: "fragmentId",
name: "...",
autoPrefixId: false, // Explicitly disabled view ID as prefix
});
-
Fragment.load({
id: "fragmentId",
name: "...",
controller: this,
});
sap.ui.xmlfragment("fragmentId", "...", this); // Deprecated
Resulting global ID: "fragmentId--controlId"
*
(Formerly sap.ui.getCore().byId("controlId")
)
... if no ID to prefix was given. The below settings are not recommended as all control IDs within the fragment definition will be registered globally without any prefix. The uniqueness of the IDs is not guaranteed!
this.loadFragment({ name: "...", autoPrefixId: false }); // Not recommended if no id
Fragment.load({ name: "...", controller: this }); // Not recommended
sap.ui.xmlfragment("demo.view.MyFragment", this); // Deprecated
Resulting global ID: "controlId"
* Do not rely on the resulting global ID, for example, concatenating ID parts manually in your application. Always use the dedicated APIs mentioned above such as byId
and createId
. See Stable IDs: All You Need to Know.
Favor model-first approach over byId
Instead of accessing the fragment controls directly, consider manipulating the UI via data binding. Changes in the model will be reflected in the UI automatically, and, if two-way binding is enabled, user inputs from the UI will be stored in the model directly.
When developing Fiori elements extensions, make sure to adhere to the documented compatibility guidelines, especially regarding byId
:
[...] Don't access or manipulate controls, properties, models, or other internal objects created by the SAP Fiori elements framework.
[...] Must not access any UI elements that are not defined within your view extensions.
β Caution
If you do not adhere to this guideline, your app may not work with future SAPUI5 versions because SAP Fiori elements might exchange controls for new ones that have a different API.