【问题标题】:How to directly bind two data properties into one control property using OData model?如何使用 OData 模型将两个数据属性直接绑定到一个控件属性中?
【发布时间】:2014-04-14 18:07:10
【问题描述】:

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

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

【问题讨论】:

标签: javascript sapui5


【解决方案1】:

另外,您可以在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>

然后使用这两个属性:

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

【讨论】:

    【解决方案2】:

    你可以使用calculated fields for data binding,例如:

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

    【讨论】:

      【解决方案3】:
      1. 将通过创建新列并添加新字段来使用视图中的计算列。

      2. 将使用 formatter 属性。请参考以下示例:

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

      【讨论】:

        【解决方案4】:

        Qualitue 和 Nikolay 的解决方案都可以正常工作。对于简单的组合装订情况,即只需将两根绳子放在一起,尼古拉的更好,因为它很简单。关键是要设置data-sap-ui-xx-bindingSyntx="complex",否则不行。对于更复杂的绑定情况,即需要先改变数据类型再绑定,Qualiture的值得一试。这是更通用的解决方案,但也更复杂。

        【讨论】:

          猜你喜欢
          • 2012-03-24
          • 2010-11-23
          • 2011-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多