首页 > 解决方案 > 如何在我的自定义 sapui5 控件中获取初始上下文路径?

问题描述

我设计了自己的自定义 sapui5 控件。该控件还用作“sap.ui.table”控件中的模板。此表包含 5 行,并绑定到大多数超过 5 行的 odata 结果集。因此,当我现在滚动浏览此表时,我必须使我的控件适应新的绑定值。但是,由于该行的绑定数据的值之间存在依赖关系,同一表行的其他控件也可以通过属性绑定来更改此控件的值。

所以我必须认识到,如果控件中的值更改是由同一行中另一个控件的更改方法通过“正常”属性设置发出的,或者它是由表滚动操作发出的。

为此,我在控件的设置器中检查绑定值的上下文路径。如果上下文路径与我存储的最后一个上下文路径不同,则更改由表滚动操作发出。如果上下文未更改,则由另一个编程操作发出(可能是同一表行中另一个控件的更改事件,该控件依赖于该控件)。

为此,我将上下文路径存储在控件的内部实例变量中。但问题是初始值。如何在我的控制下保存初始的、第一个绑定的上下文路径。不可能并且允许在我的控件的构造函数中检查它。最初,滚动和程序更改将返回上下文更改。

这是我的控件的简化代码片段,以便更好地理解。因为语法错误的简单借口。

sap.ui.define(["sap/ui/core/Control",
    "sap/ui/model/Filter",
    "sap/ui/model/FilterOperator"   
], function (Control, Filter, FilterOperator) {
    "use strict";
    return Control.extend("MyControl", {
        "metadata": {
            "properties": {
                "value"                         : { type : "string" }
            }
        },

        init: function () {
            this._lastContextPath       = "";   // should store the last context path 
        },
        
        
        setValue : function(oValue) {
            // Setter Method for my value Property
            this.setProperty("value1",oValue);
            // Check if setter was issued by scrolling if this control 
            // is bound to a table
            if (this.getId().indexOf("clone") > 0) // Simplified recognition if control is created by template
                if ( !this._hasContextChanged() ) {
                    // Do something in the parent-control where this Control is in its aggregation
                    // because Context not changed, so Setter is issued programmtically
                    if (typeof this.getParent() !== 'undefined' && this.getParent() !== null) {
                        // In this case the Parent-Method refreshFromChild should be invoke with the 
                        // value of this control
                        this.getParent.refreshFromChild(oValue);
                    }
                }
            }   
        },

        // Checks if a context change has happend
        _hasContextChanged : function() {
            var oBinding  = this.getBinding("value");
            if ( typeof oBinding !== 'undefined' && oBinding !== null) {
                if (this.getId().indexOf("clone")>0) { // Simplified recognition if control is created by template
                    var oContext     = oBinding.getContext();
                    var sContextPath = oContext.sPath;
                    if (sContextPath !== this._lastContextPath ) {
                        // Context changed. Normally by a table scroll operation
                        this._lastContextPath = sContextPath;
                        return true;
                    }
                    else {
                        return false;
                    }
                }
                else {
                    return false;
                }
            }
            else {
                return false;
            }
        }
    })
)

标签: sapui5

解决方案


推荐阅读