1

In our SAPUI5 application we have an Input field in a pages header that looses its focus.

To explain it in more detail. We have an initial view where the user enters some values. On accepting the values the application goes to the next view.

So what we want to do is set the focus to the input field in that view. We used the following code to achieve that:

jQuery.sap.delayedCall(500, this, function() {
    this.getView().byId("idInput").focus();
 });

This works fine for our inital view. On the following views though you can see the focus is set properly but the very next moment it is lost.

How can I keep the focus on that input field?

See: SAPUI5 Set Focus on Input Field

Alex K
  • 100
  • 3
  • 15

1 Answers1

0

What helped was to increase the delay. Using onAfterRendering works only the first time navigating to the second view.

Alex K
  • 100
  • 3
  • 15
  • Yes, setting focus in `onAfterRendering` works only once when the view gets rendered. When the user navigates back, however, the focus is not set again as the view didn't rerender itself. – Boghyon Hoffmann Feb 01 '18 at 10:45
  • Also avoid `delayedCall` / `setTimeout` with ms higher than `0`. Instead, make use of appropriate events such as [`afterShow`](https://ui5.sap.com/#/api/sap.m.NavContainerChild/events/AfterShow), [`afterOpen`](https://ui5.sap.com/#/api/sap.m.Dialog/events/afterOpen), `after*`, ... depending on which control is used as a container. E.g. If you have NavContainer: https://stackoverflow.com/a/48559689/5846045 – Boghyon Hoffmann Feb 01 '18 at 10:49
  • @BoghyonHoffmann That's some useful information. Thanks! – Alex K Nov 21 '18 at 12:37