I have a login view. When I press login button I call handleFooterBarButtonPress
function.
How can I manage the enter-key action to call the same function when I press enter when I am in password field?
<core:View xmlns:core="sap.ui.core" xmlns:mvc="sap.ui.core.mvc" xmlns="sap.m" xmlns:l="sap.ui.layout" xmlns:f="sap.ui.layout.form"
controllerName="view.login" xmlns:html="http://www.w3.org/1999/xhtml" >
<Page title="WEBAPP">
<content>
<FlexBox
alignItems="Center"
justifyContent="Center">
<items>
<Image src="general/img/logo.png" width="{/widthL}"/>
</items>
</FlexBox>
<FlexBox
alignItems="Center"
justifyContent="Center">
<items>
<l:Grid
defaultSpan="L12 M12 S12"
width="auto">
<l:content>
<f:Form id="loginForm"
minWidth="1024"
maxContainerCols="2"
editable="true">
<f:title>
<core:Title text="" />
</f:title>
<f:layout>
<f:ResponsiveGridLayout
labelSpanL="3"
labelSpanM="3"
emptySpanL="4"
emptySpanM="4"
columnsL="1"
columnsM="1" />
</f:layout>
<f:formContainers>
<f:FormContainer>
<f:formElements>
<f:FormElement label="Username">
<f:fields>
<Input id="id_inputUsername" />
</f:fields>
</f:FormElement>
<f:FormElement label="Password">
<f:fields>
<Input id="id_inputPassword" type="Password" />
</f:fields>
</f:FormElement>
</f:formElements>
</f:FormContainer>
</f:formContainers>
</f:Form>
</l:content>
</l:Grid>
</items>
</FlexBox>
</content>
<footer>
<Bar>
<contentRight>
<Button id="idButtonLogin" text="Login" type="Emphasized" visible="true" press="handleFooterBarButtonPress" icon="sap-icon://accept" />
</contentRight>
</Bar>
</footer>
</Page>
</core:View>
On an sap.m.Input
control, enter causes the change
event to be fired. So you can set a handler on that, and that handler can fire the press event* on the sap.m.Button
control.
View:
<Input id="id_inputPassword"
type="Password"
change="onPasswordChange" />
...
<Button id="idButtonLogin"
text="Login"
type="Emphasized"
visible="true"
press="handleFooterBarButtonPress"
icon="sap-icon://accept" />
Controller:
onPasswordChange : function(oEvent) {
this.getView().byId("idButtonLogin").firePress();
},
handleFooterBarButtonPress : function(oEvent) {
jQuery.sap.require("sap.m.MessageToast");
sap.m.MessageToast.show("Login!");
}
Here's a live example courtesy of JSBin.
warning: firePress is protected, so you may want to do something different
it work, but I have the same problem I have explained below: when I press Enter key I have double execution of code!!
Hi qmacro, your code will trigger Login event handler when the input is lost focus.
Sorry qmacro. It work! ;) But why if i use "attachBrowserEvent" the function associated fire twice? Thankyou!
Allen, this is true, but also perhaps what is wanted in this case.
qmacro, it will result in unexpected login issue. please see my answer.