cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Call dynamic function by name SAPui5

lokijana
Participant
0 Kudos
759

Hi Experts,

I am adding sap.m.Link to my controller with dynamic text and on press function.

I have function name onSampleOnePressed:function({window.open("google.com");}); and onSampleTwoPressed:function({window.open("answer.sap.com");});

Here is my code

var aData = [{
    text: "Sample1",
    actionName: "onSampleOnePressed"
},
    text: "Sample2",
    actionName: "onSampleTwoPressed"
}
aData.forEach(function(data) {
      new sap.m.Link({
	    text: data.text
	    press: this.+data.actionName            
            //press: "this." + data.actionName --error: "this" converted to string not a function
      })
});

the dynamic text is shows properly, I'm having trouble how to make press function call dynamically.

When tried to change press to press: data.actionName I'm getting error [TypeError: I.fFunction.call is not a function]

I tried to change to static press to press: this.onSampleTwoPressed to but the sample2 Link same with sample1.

Accepted Solutions (0)

Answers (1)

Answers (1)

Dominik-deluxe
Explorer
aData.forEach(function (data) {
    new sap.m.Link({
        text: data.text
        press: (oEvent) => {
            const sFunctionName = oEvent.< getYourActionNameHere >
                this[sFunctionName]()  // expect the function to be on the same controller 
        }
    })
});

lokijana
Participant
0 Kudos

do you mean const sFunctionName = oEvent.data.actionName?

what is <getYourActionNameHere>?

I have two action name which is onSampleTwoPressed, and onSampleOnePressed on same controller

I got error on arrow function but I rewrite code to this;

 press: function(oEvent) {
            const sFunctionName = data.actionName;
                this[sFunctionName](); 
        }

and still having an error [this[sFunctionName] is not a function]

lokijana
Participant
0 Kudos

Hi Dominik, I appreciate your comment.

I solved my problem, your code became my basis! Thank you!