How to pass parameters to a event handlerr in XML Views SAP UI5

Deepak picture Deepak · Aug 7, 2014 · Viewed 20.3k times · Source

I am having trouble sending data from the XML view to controller. It is easily achievable in JS views.

for example:-

In JS view:-

var btn = new sap.m.Button({
    text:"click",
    tap:function(){
          callFunction(oEvent, "mycustomString");
    }
});

How do I achieve the same using XML Views.

<Button text="click" tap="callFunction"/>

The above would only pass the event and not the "mycustomString". How to I do this?

Answer

Benvorth picture Benvorth · Dec 31, 2015

You can do this by adding a custom data-parameter app:myData in your view:

View

<mvc:View
    ...
    xmlns:app="http://schemas.sap.com/sapui5/extension/sap.ui.core.CustomData/1"
    ...
>
    <Button text="click" tap="callFunction" app:mydata="mycustomString"/>
    ...

Controller

callFunction: function(oControlEvent) {
    console.log("data: " + oControlEvent.getSource().data("mydata"));
}

logs data: mycustomString

See https://scn.sap.com/thread/3538029