on 02-27-2019 10:58 AM
Hi folks,
I want to create the following mentioned scenario with the use of SAP UI5 framework (on SAP Web IDE):
Now, I have faced below mentioned issues:
Also I don’t want the smiley to be loaded when the page gets loaded. What is the rectification in code that I need to do. I want the smiley after I press OK of the alert box.
Any further suggestions are highly appreciated. Expecting once again boghyon.hoffmann to be amongst the first to help me out (Just kidding :P).
Find below the snippets of ‘index.html’ , ‘View’ & ‘controller’
1) index.html
<!DOCTYPE HTML>
<html>
<head>
<script id="sap-ui-bootstrap"
src="https://sapui5.hana.ondemand.com/resources/sap-ui-core.js"
data-sap-ui-libs="sap.ui.commons"
data-sap-ui-theme="sap_bluecrystal"
data-sap-ui-resourceroots='{
"pdbhai": "./"}'>
</script>
<script>
var oExerciseView = new sap.ui.view({
id:"idExercise",
viewName:"pdbhai.view.ExerciseXMLView",
type: "XML"
});
var oSmileyView = new sap.ui.view({
id:"idSmiley",
viewName:"pdbhai.view.SmileyView",
type:"XML"
});
oExerciseView.placeAt("content");
oSmileyView.placeAt("smiley");
</script>
</head>
<body class="sapUiBody">
<div id="content">
</div>
<div id="smiley">
</div>
</body>
</html>
2) .view1.xml
<mvc:View xmlns:core="sap.ui.core"
xmlns:mvc="sap.ui.core.mvc"
xmlns="sap.ui.commons"
controllerName="pdbhai.controller.ExerciseXMLView"
xmlns:html="http://www.w3.org/1999/xhtml"
xmlns:lt="sap.ui.commons.layout">
<lt:MatrixLayout id="myLayout"
xmlns="sap.ui.commons" layoutFixed="true" columns="2"
width="50%">
<lt:MatrixLayoutRow>
<lt:MatrixLayoutCell><Label text="User name"></Label></lt:MatrixLayoutCell>
<lt:MatrixLayoutCell><TextField id="user"></TextField></lt:MatrixLayoutCell>
</lt:MatrixLayoutRow>
<lt:MatrixLayoutRow>
<lt:MatrixLayoutCell><Label text="Password"></Label></lt:MatrixLayoutCell>
<lt:MatrixLayoutCell><TextField id="pwd"></TextField></lt:MatrixLayoutCell>
</lt:MatrixLayoutRow>
<lt:MatrixLayoutRow>
<lt:MatrixLayoutCell>
<Button id="idBtn" text="Verify" press="OnPress"></Button>
</lt:MatrixLayoutCell>
</lt:MatrixLayoutRow>
</lt:MatrixLayout>
</mvc:View>
------------------------------------------------------------------------------------------
3) view2.xml
<mvc:View xmlns:core="sap.ui.core"
xmlns:mvc="sap.ui.core.mvc"
xmlns="sap.ui.commons"
controllerName="pdbhai.controller.ExerciseXMLView"
xmlns:html="http://www.w3.org/1999/xhtml">
<Image src="Smiley.jpg"></Image>
</mvc:View>
----------------------------------------------------------------------------------------------
4) .controller.js
/*eslint-disable no-console, no-alert */
sap.ui.define([
"sap/ui/core/mvc/Controller"
], function (Controller) {
"use strict";
return Controller.extend("pdbhai.controller.ExerciseXMLView", {
OnPress : function() {
var oInp1 = sap.ui.getCore().byId("user").getValue();
var oInp2 = sap.ui.getCore().byId("pwd").getValue();
if (oInp1 === "prathamesh" & oInp2 === "deshmukh") {
alert("Verified");
}
else {
alert("Not Verified");
}
}
});
});
Hi,
the proper way is not to get value from the controls and bind them to a model.
your controller can be like this
sap.ui.define([
"sap/ui/core/mvc/Controller",
"sap/ui/model/json/JSONModel"
], function (Controller, JSONModel) {
"use strict";
return Controller.extend("pdbhai.ExerciseXMLView", {
onInit: function() {
this.getView().setModel(new JSONModel({
user: "",
pwd: ""
}));
},
OnPress : function() {
var oModel = this.getView().getModel();
var oUser = oModel.getProperty("/user");
var oPassword = oModel.getProperty("/pwd");
if (oUser === "prathamesh" & oPassword === "deshmukh") {
alert("Verified");
} else {
alert("Not Verified");
}
}
});
});
and view can be like this
<mvc:View xmlns:core="sap.ui.core"
xmlns:mvc="sap.ui.core.mvc"
xmlns="sap.ui.commons"
controllerName="pdbhai.ExerciseXMLView"
xmlns:html="http://www.w3.org/1999/xhtml"
xmlns:lt="sap.ui.commons.layout">
<lt:MatrixLayout id="myLayout" xmlns="sap.ui.commons" layoutFixed="true" columns="2" width="50%">
<lt:MatrixLayoutRow>
<lt:MatrixLayoutCell><Label text="User name"></Label></lt:MatrixLayoutCell>
<lt:MatrixLayoutCell><TextField id="user" value="{/user}"></TextField></lt:MatrixLayoutCell>
</lt:MatrixLayoutRow>
<lt:MatrixLayoutRow>
<lt:MatrixLayoutCell><Label text="Password"></Label></lt:MatrixLayoutCell>
<lt:MatrixLayoutCell><TextField id="pwd" value="{/pwd}"></TextField></lt:MatrixLayoutCell>
</lt:MatrixLayoutRow>
<lt:MatrixLayoutRow>
<lt:MatrixLayoutCell>
<Button id="idBtn" text="Verify" press="OnPress"></Button>
</lt:MatrixLayoutCell>
</lt:MatrixLayoutRow>
</lt:MatrixLayout>
</mvc:View>
and please use sap.m controls and not sap.ui.commons because the latter is deprecated. https://blogs.sap.com/2016/07/25/end-of-road-for-sapuicommons-library/
Working Example: Link
hope this help
Thanks
Dennis
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Dennis,
Thank you for the time. I appreciate it. This is what I needed at the time.
Thank you once again. Keep sharing the knowledge !
Hi Himanshu,
The line is : (in the controller)
and I have tried with && as well. No success.
var oInp1 =sap.ui.getCore().byId("user").getValue();
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
var oInp1 = this.getView().byId("user").getValue();
and you should be fine.
Hi Prathmesh,
& is not an operator in javascript, try with && as below code.
if(oInp1 ==="prathamesh" && oInp2 === "deshmukh") {
alert("Verified");}else{
alert("Not Verified");}
I am not able to see getValue() method in controller, please suggest the line where it is causing error ?
Thanks,
Himanshu Pensia
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
90 | |
10 | |
10 | |
10 | |
7 | |
7 | |
6 | |
5 | |
4 | |
3 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.