javascript 如何使用 OData 模型将两个数据属性直接绑定到一个控件属性中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22320475/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 22:55:07  来源:igfitidea点击:

How to directly bind two data properties into one control property using OData model?

javascriptsapui5

提问by Dr. Dong Zhu

I am using an OData model to bind UI controls to GW services. In the service metadata, there are, say, "FirstName" and "LastName" in the data structure. On the UI, say, I am using a Label control.

我正在使用 OData 模型将 UI 控件绑定到 GW 服务。在服务元数据中,例如数据结构中有“FirstName”和“LastName”。例如,在 UI 上,我使用的是 Label 控件。

Now the question is how to bind the Text property of Label to a string of "FullName" (which is "FirstName"+"LastName") using OData Model directly? If I use the JSON model, I can create a local variable, FullName = FirstName + LastName, and bind the Text property to FullName. But how could I do this using OData Model?

现在的问题是如何直接使用OData模型将Label的Text属性绑定到一个字符串“FullName”(即“FirstName”+“LastName”)?如果我使用 JSON 模型,我可以创建一个局部变量FullName = FirstName + LastName,并将 Text 属性绑定到 FullName。但是我怎么能用 OData 模型做到这一点呢?

回答by Nikolay Nadorichev

Additionally, you can enable complex data binding in sap-ui-core.js:

此外,您可以在sap-ui-core.js 中启用复杂数据绑定:

<script src="resources/sap-ui-core.js"
        id="sap-ui-bootstrap"
        data-sap-ui-libs="sap.ui.commons,sap.ui.table,sap.ui.ux3"
        data-sap-ui-xx-bindingSyntax="complex"></script>

And then use both properties:

然后使用这两个属性:

var oLabel = new sap.ui.commons.Label({
        text : "{firstName} {lastName}"
});

回答by Qualiture

You could use calculated fields for data binding, for instance:

您可以使用计算字段进行数据绑定,例如:

var oLabel = new sap.ui.commons.Label()
.bindProperty("text", {
  parts: [
    {path: "/firstName", type: new sap.ui.model.type.String()},
    {path: "/lastName", type: new sap.ui.model.type.String()}
  ],
  formatter: function(firstName, lastName){
    return firstName + " " + lastName;
  }
});

回答by Sergio Guerrero

  1. would be to use a Calculated Column from the view by creating a new column and adding the new field.

  2. would be to use the formatter property. please refer to the example below:

    new sap.ui.commons.TextView ({ 
        text: {  path: o.value, // o is an object in my local scope
            formatter: function(i) {
                // after getting your col1 and col2 values you could do the following
                var yourConcatValue = col + col2;
                return yourConcatValue;
            }                                
       }                          
    });
    
  1. 将是通过创建一个新列并添加新字段来使用视图中的计算列。

  2. 将使用格式化程序属性。请参考以下示例:

    new sap.ui.commons.TextView ({ 
        text: {  path: o.value, // o is an object in my local scope
            formatter: function(i) {
                // after getting your col1 and col2 values you could do the following
                var yourConcatValue = col + col2;
                return yourConcatValue;
            }                                
       }                          
    });
    

回答by Dr. Dong Zhu

Both Qualitue and Nikolay's solutions work fine. For simple combined binding case, ie, just put two strings together, Nikolay's is better as it is simple to do. The key point is to set the data-sap-ui-xx-bindingSyntx="complex", otherwise, it wno't work. For more complicated binding cases, ie, need to change the data type first and then binding, Qualiture's is worth doing. It is more general solution but also more complex to do.

Qualitue 和 Nikolay 的解决方案都运行良好。对于简单的组合装订情况,即只需将两个字符串放在一起,Nikolay's 更好,因为它很简单。关键是要设置data-sap-ui-xx-bindingSyntx="complex",否则不起作用。对于更复杂的绑定情况,即需要先改变数据类型再绑定,Qualiture的值得做。这是更通用的解决方案,但也更复杂。