Hello everyone
,
This is my first blog,
In it, I am about to explain How to pass the parameter to function in the controller from XML view in SAP UI5. I hope my blog will help.
most of the time we write separate (multiple) functions for performing actions based on how and where we are triggering the object or action. in simple words, we are passing the parameters to the function at the time function call.
let's create a scenario for it suppose we have two buttons one is
Save as Draft
and the second is
Submit
.
suppose on Save as draft we are sending the API call with Record id 1 and on Submit we are sending it with 0.
for these, we usually write two functions and write the other logic in the same manner.
but with the help of passing parameters to a function we can achieve it with a single function.
let's get started.
1.
Introduction
Before starting let me explain why and in which case we can use these pass parameters from XML method. -
-
To reduce the
number of calls to internal functions which helps to achieve different conditions.
-
function functionality is dependent upon a particular parameter.
-
We can you this in functions in which logic is the same but depends on parameters conditions are changing and functionality is the same.
2.
Prerequisite
3.
Controller Code
In the controller, we write the function logic and conditions required to perform the action as per the event triggered.
Here is one of the functions which perform the action required as per condition or parameter.
onPressFun: function (oEvent, SecondPara, sActionValue) {
var BtnEvent = oEvent.getSource();
var SecondParameter = SecondPara;
if (SecondParameter === "1") {
sap.m.MessageBox.information(sActionValue);
} else if (SecondParameter === "0") {
sap.m.MessageBox.information(sActionValue);
} else {
sap.m.MessageBox.information("Wrong data Button event triggered");
}
}
4.
XML Code
From the XML View function, we need to pass the parameter where we are calling the function like mentioned in below XML code.
with the help of parameter in function call we can avoid multiple functions which have the same logic but depends upon a different parameter.
See the code below