SAPUI5 XML视图使用来自同一模型

时间:2017-07-14 10:39:23

标签: xml routing sapui5

我有以下问题:

我想开发购物车并且产品卡的计数器出现问题,我在摘要视图中显示数据时遇到问题。

对于这个项目,我使用的是XML视图,而且我已经对绑定有很多了解。当我想绑定一个静态路径时,我没有问题。数据来自名为" cartData"的JSON模型。

示例(来自goToCart按钮)

...
text="{cartData>/currentUser}";
...

所有内容都正确显示(在示例中),但对于我的项目,我需要绑定主绑定(对于购物车的计数器),此路径需要用户的参数。它保存在路径中,如示例中所示。

我已经尝试了很多组合来完成这个bug,但现在我没有更多的想法: - (

我尝试过的组合的一个例子:

text="{ ${cartData>/cartOfUser/} + {cartData>/currentUser} + '/roles/counter'}"

修改

我的代码的一些虚拟部分:

我的按钮(我的工作还没有......):

<m:Button
                id="details.Btn.ShowCart"

                text="{ parts: [
                    {path: 'cartProducts>/cartEntries/'},
                    {path: 'cartProducts>/currentChoice/'},
                    {path: '/addedRoles/counter'}
                ]}"

                type="Emphasized"
                icon="sap-icon://cart-3"
                iconFirst="true"
                width="auto"
                enabled="true"
                visible="true"
                iconDensityAware="false"
                press="showCart"/>

我在LocalStorage中的JSON模型如何:

{
    "cartEntries": {
        "counter": 2,
        "UserId12": {
            "UserId": "UserId12",
            "Email": "Email12",
            "dateCreated": "2017-07-14T13:18:13.632Z",
            "dateUpdated": "2017-07-14T13:18:13.632Z",
            "addedRoles": {
                "counter": 0
            },
            "existingRoles": {
                "counter": 0
            }
        },
        "UserId14": {
            "UserId": "UserId14",
            "Email": "Email14",
            "dateCreated": "2017-07-14T13:18:30.415Z",
            "dateUpdated": "2017-07-14T13:18:30.415Z",
            "addedRoles": {
                "counter": 0
            },
            "existingRoles": {
                "counter": 0
            }
        }
    },
    "currentChoice": "UserId14"
}

我的JSON数据评论: I need to grab the value from "currentChoice", to search with this information in cartEntries for the right counter

Button现在看起来如何: It show the data not in the correct way. Please ignore the zero at first...

目标是获取&#34; currentChoice&#34;的价值。并将其用作参数&#39;为正确的用户调用信息..

我也尝试过:

text="{= ${= 'cartProducts>/cartEntries/' + ${cartProducts>/currentChoice/} + '/addedRoles/counter' } }"

什么有效,但我需要更多&#34;动态&#34;是:

text="{cartProducts>/cartEntries/UserId14/addedRoles/counter}"

我希望你们现在知道我的意思了......: - /

祝你好运

解决方案

我如何解决问题:

  1. 在按钮上添加格式化程序:

      

    /&#39 ;,                     格式化程序:&#39; .formatter._getCartInt&#39;                 }&#34;                 类型=&#34;强调&#34;                 图标=&#34; SAP-图标://车-3&#34;                 iconFirst =&#34;真&#34;                 宽度=&#34;自动&#34;                 启用=&#34;真&#34;                 可见=#&34;真&#34;                 iconDensityAware =&#34;假&#34;                 按=&#34; showCart&#34; /&GT;

  2. 在我的formatter.js文件中实现格式化程序:

      

    _getCartInt:function(sP1){             var sCurrent = sP1.currentChoice;             var sFinalString =&#34; cartProducts&gt; / cartEntries /&#34; + sCurrent +&#34; / addedRoles / counter&#34 ;;             this.getView()。byId(&#34; btn.ShowCart&#34;)。bindProperty(&#34; text&#34;,{path:sFinalString,type:new sap.ui.model.type.Integer() }); }

2 个答案:

答案 0 :(得分:1)

尝试使用以下方法:

在i18n文件中

cartInfoTitle=User: {0} has: {1} items in the cart
XML视图中的

<Text text="{
    parts: [
        {path: 'i18n>cartInfoTitle'}, 
        {path: 'modelName>/property1'}, 
        {path: 'modelName>/property2'}
    ], 
    formatter: 'jQuery.sap.formatMessage'
}" />

因此,您声明i18n条目,然后使用预定义的格式化程序将占位符替换为&#34;部分&#34;的值。数组(Documentation article)。

答案 1 :(得分:0)

好的回答:你不能在绑定中使用表达式(同样适用于类)。因此,要获得您想要的输出,您确实需要格式化程序+在绑定部分中包含JSON模型所需的顶级元素(以便正确更新)。

XML(我假设您的模型名为'cartData')

<Text text="{
    parts: [
        'cartData>/cartEntries',
        'cartData>/currentChoice'
    ], 
    formatter: '.myFormatter'
}" />

JS控制器

controller.prototype.myFormatter = function (cartEntries, currentChoice) {
    if (cartEntries && cartEntries[currentChoice]) {
        return cartEntries[currentChoice].addedRoles.counter;
    }
}

[未经过测试的代码]