Search code examples
bindingsapui5

SAPUI5 Dynamic Databinding (Key for OData-Service)


I am trying to connect my UI5 Application to a OData-Service.

For the binding i use databinding (The 'odataModel' is my OData-Service):

<Text text="{odataModel>/UserSet('MyUsername')/UserName}"/>

Now i want to replace the 'MyUsername' String within the XML, therefore i am using another model. So i tried the following:

<Text text="{odataModel>/UserSet('${userModel>/user}')/UserName}"/>

How do i fit a variable into my binding?

Greetings


Solution

  • You need to do Element Binding, and set the context from the controller for the control or container you want. Just set your binding in the view relative (no slash at the begining) to the "/UserSet('MyUsername')" node and give an ID to the control or container where you want to do ElementBinding. For example:

    <Text id="myText" text="{odataModel>UserName}"/>
    

    Then from the controller, you can do ElementBinding whenever you have the username string, for example:

    onInit(){
       var sUsername = this.getView().getModel("userModel").getProperty("user"); // This is your username coming from your "userModel"
       var sModelNodeAbsolutePath = "odataModel>/UserSet("+ sUsername +")"; // This is the absolute path to the node for this user, the path in the view will be relative to this node
       this.getView().byId("myText").bindElement(sModelNodeAbsolutePath);
    }