cancel
Showing results for 
Search instead for 
Did you mean: 

How to give focus for input box in sapui5 mobile application

former_member198924
Participant
0 Kudos

I have give focus for input field particularly one input field. I tried someway but that's not working. please assist me.

Accepted Solutions (0)

Answers (7)

Answers (7)

Former Member
0 Kudos

Hi Venkatesh,

SAPUI5 provides mechanisms for observing the moving focus in an application page for controls. This information is then preserved for refocusing elements after

rerendering. The focus triggers event firing. However, due to the high degree of flexibility in control rendering,

a functionality tailored to the respective controls is required. For this, the framework provides helper functions for the implementation of focus handling.

Each control provided by the SAPUI5 framework has its own behavior for focus handling, depending on the functionality that is provided by the control.

Complex controls and their embedded content constitute the highest level of complexity.

The base class for elements (Element.js) provides the following four methods to support the implementation of focus handling:

Element.getFocusDomRef()

Once a visible element is rendered, it has a Document Object Model (DOM) representation. The root DOM node can be accessed by using the method getDomRef()

on the element. The root DOM node is the default focused DOM node. After rendering, when a control is supposed to be focused, the framework asks the control for its

focus DOM node by using the getFocusDomRef() method. If the root DOM node does not represent the element that should have the focus, you have to return another DOM

node by overriding the getFocusDomRef() method.

Element.focus()

The focus() method sets the focus on the element. This is done using the focus DOM node.

Element.getFocusInfo()

For some controls, it is even more difficult to apply the focus once the control has been rerendered. List controls, for example, have their own internal focus

handling and set the focus on the different items. A data table moves the focus over a matrix of cells. The requirement is that a control can apply the focus to

its exact previous position after rerendering. In cases where the SAPUI5 rendering mechanism fails to find the correct element after rendering (for example because

it does not have an ID or the ID changed), override the getFocusInfo() method and serialize the focus state into a JSON object and return it. Before rendering, the

render manager calls this method for the element instance and stores this information for future use. After rendering, it calls the applyFocusInfo() methodand passes

back the serialized object. This is not only useful for focus information: The exact cursor position of a TextField control, for example, can also be stored in such an object.

Element.applyFocusInfo(oFocusInfo)

The applyFocusInfo() method applies the focus to the element after rerendering. You use this method if a different behavior is expected for the element.

I hope it's useful to you.

Former Member
0 Kudos

$(document.ready(function()

{

sap.ui.getCore().byId("inputId").focus();

}));

Former Member
0 Kudos

You have to set a timeout:

jQuery.sap.delayedCall(0, this, function() {
  this.byId("input").focus()
});

former_member198924
Participant
0 Kudos

Hi Stephanie,

                       Thank you for your response. I'll try and let you know.

Former Member
0 Kudos

use a jquery focus method.

$("id-of-the-textfield").focus()

Former Member
0 Kudos
Former Member
0 Kudos
Qualiture
Active Contributor
0 Kudos

Have you tried the focus() method as mentioned in the API?

former_member198924
Participant
0 Kudos

I tried that method but its not working. For Example:

var input = new sap.m.input("",{placeholder:"Name"});

input.focus();

santhu_gowdaz
Active Contributor
0 Kudos

At what time your input value to be focus??

if it is in initial then,

in view,

var input = new sap.m.input("inputId",{placeholder:"Name"});

in controller,

init(),

var getInputId = sap.ui.getCore().byId("inputId");

getInputId.focus();

former_member198924
Participant
0 Kudos

i have two view. when i come to second view that time. i have to give focus to input field.

santhu_gowdaz
Active Contributor
0 Kudos

then in 2nd view init() method do coding.

Qualiture
Active Contributor
0 Kudos

placing that code in the init() hook is a bad idea, since that will only be called once in the entire view lifecycle.

A better place is in the onAfterRendering() event handler

AndreasKunz
Advisor
Advisor
0 Kudos

> var input = new sap.m.input("",{placeholder:"Name"});

> input.focus();

Price question: WHERE is the Input being displayed at the time you call .focus() ?

Make sure to call focus when the DOM is actually there, including the DOM of the Input.

Some suggestions are already floating around, but to really help one needs to know the exact use-case better. In a controller's init() method is not a good place (before rendering). The afterRendering hook will work, but I doubt you want to have the focus always only in the Input, and rerendering of the View will then put it there.

Regards

Andreas