自定义UI5控件无法更新绑定值。

3

我如何把OData的值传递给自定义UI5控件的属性?我尝试了这个方法,但是它没有生效
如果我尝试将它传递给普通控件,例如<Text>,它就有效
如果我尝试传递静态值,它也有效

<cc:CheckPrice valuePrice="{Price}"/> <!--❌-->
<Text text="{Price}"/> <!--✔️-->
<cc:CheckPrice valuePrice="1000"/> <!--✔️-->

如何将远程OData服务中的值传递并显示在文本控件中?
渲染自定义控件的截图
sap.ui.define([
    "sap/ui/core/Control",
    "sap/m/Label",
    "sap/m/Button"
], function (Control, Label, Button) {
    "use strict";

    return Control.extend("zgutfiory.zguttestfiorylr.controls.CheckPrice", {
        metadata: {
            properties: {
                "valuePrice": {
                    type: "string",
                    bindable: "bindable"
                }
            },
            aggregations: {
                "_label": {
                    type: "sap.m.Label",
                    multiple: false,
                    visibility: "hidden"
                },
                "_button": {
                    type: "sap.m.Button",
                    multiple: false,
                    visibility: "hidden"
                }
            },
            events: {
                // ...
            }
        },

        init: function () {
            this.setAggregation("_label", new Label({
                text: "{i18n>controlCheckPrice}"
            }).addStyleClass("sapUiSmallMargin"));
            this.setAggregation("_button", new Button({
                text: "{i18n>controlCheckPrice}",
                press: [this._onSubmit, this]
            }).addStyleClass("sapUiTinyMarginTopBottom"));
        },

        _onSubmit: function (oEvent) {
            // ...
        },

        renderer: function (oRm, oControl) {
            oRm.write('<div class="zgutCheckPriceWrap">');
            oRm.write('<div class="zgutPriceWrap">' + oControl.getValuePrice() + '</div>');
            oRm.renderControl(oControl.getAggregation("_label"));
            oRm.renderControl(oControl.getAggregation("_button"));
            oRm.write('</div>');
        }
    });
});

UI5版本:1.92

3个回答

2
您在渲染函数中漏掉了一些必要的控件数据。当模型值发生变化(例如,ODataModel接收到数据)时,绑定属性的控件需要至少可以通过一个id进行识别,以便更新该控件。在您的情况下,id没有被渲染出来。
为添加所需的控件数据,请使用以下方法:
  • oRm.openStart("div", oCotrol) 适用于新的语义化渲染(UI5≥1.67)。
  • oRm.writeControlData(oControl) 适用于已弃用的基于字符串的渲染(UI5≤1.66)。注意:请在此处使用.writeEscaped来避免XSS,例如oControl.getValuePrice()
此外,如果您重写了借用的方法(例如init),请确保调用超级函数。这对于生命周期钩子非常重要。
示例:https://embed.plnkr.co/gnmPoh3KS7L3DDXZ?show=controls%2FCheckPrice.js,preview
有关更多信息,请参见API参考:sap/ui/core/RenderManager

1
自定义控件的绑定与标准控件完全相同。必须考虑以下几点:
  • 如果父容器(例如视图)已绑定了上下文,则必须提供您正在绑定值的属性的相对路径,例如{Price}。否则,请提供绝对路径,例如{/Price}
  • 如果模型已命名,请确保在绑定时提供名称。例如{namedModel>/Price}
  • 路径也可以是多级的。例如,对于使用NavigationProperty ToCategory的OData属性绑定,可以使用{ToCategory/CategoryName}

1
谢谢,但它不起作用 - 或许我在控件上做错了什么?使用文本控件可以工作,但是使用我的控件就不行: <Text text="{Price}"/> <cc:checkPrice id="checkPrice" valuePrice="{Price}" class="sapUiSmallMarginBeginEnd" /> - tanto39

-1

看起来你可能只是缺少了前导斜杠?

<cc:checkPrice id="checkPrice" valuePrice="{/Price}" class="sapUiSmallMarginBeginEnd" />


1
不,加上斜杠也不起作用。 <Text text="{Price}"/> - 起作用了,但我的控件却不起作用。 - tanto39

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接