I have a Detail view in an SAPUI5 app which contains a single input field with ID "bestellmenge_tu"
. Whenever this view is called, the focus should be on that input field. Unfortunately, when setting the focus on the field in the controller's onInit
method, the focus will be set but some milliseconds later the UI5 takes it away and transfers it to the "navigation back" button of the detail view.
By putting a log.trace()
on the input field's blur
event, I found out that the focus is taken away by a method called sap.ui.define.NavContainer._afterTransitionCallback
which is called asynchronously (some window.setTimeout
s between trigger and execution). The function simply looks for the first focusable element in the view and brutally switches the focus on it.
My workaround was to redefine the method jQuery.fn.firstFocusableDomRef
which is used to find this "first focusable element":
// DIESE KANONE FUNKTIONIERT
jQuery.fn.firstFocusableDomRef = (function() {
var _default = jQuery.fn.firstFocusableDomRef;
return function() {
var bestellmenge_tu = document.querySelector("input[id$='bestellmenge_tu-inner']");
if (bestellmenge_tu &&
bestellmenge_tu.style.display !="none" &&
bestellmenge_tu.style.visibility != "hidden") return bestellmenge_tu;
else return _default.apply(this);
}
})();
But this could be a performance issue (querySelector
called during DOM transversal at any page load from there on), and it is too much coding for the desired effect.
Is there an easier method to achieve this?
I thought of something like
<mvc:View controllerName="zrt_dispo.view.Detail"...>
<Page id="detailPage" initialFocus="bestellmenge_tu"> <!-- ID of the element to carry the focus -->
</Page>
</mvc:view>
oElementYouWantToFocus
created? Is it part of an asynchronously created fragment? Is the app generated by Fiori elements? V2? V4? – Giles