SAPUI5 Dynamic Databinding (Key for OData-Service)

匆匆过客 提交于 2019-12-13 03:09:43

问题


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


回答1:


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);
}


来源:https://stackoverflow.com/questions/51188215/sapui5-dynamic-databinding-key-for-odata-service

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!