如何将控件引用传递给 XMLView 中的格式化程序?

Qua*_*ure 3 sapui5

在 SAPUI5 的 JSView 中,将当前控件引用传递给格式化函数非常容易:

oTable.bindItems("/rows", new sap.m.ColumnListItem({
    cells : [ new sap.m.Text().bindProperty("text", {
        parts: [
            { path: "someInteger" }
        ],
        formatter: function(iValue) { 
            var idText = this.getId(); //this references the current control
            return iValue;
        }
    })]
}));
Run Code Online (Sandbox Code Playgroud)

(当然,“简单”部分是因为this在控件的内部格式化程序函数中引用了)

但是,对于 XMLViews,我还没有设法在格式化程序函数中获取对当前控件的引用:

<Table items="{/rows}">
    <columns>
        <Column>
            <Text text="Some Integer" />
        </Column>
    </columns>
    <items>
        <ColumnListItem>
            <cells>
                <Text text="{ path : 'someInteger', formatter : '.formatCell' }" />
            </cells>
        </ColumnListItem>
    </items>
</Table>
Run Code Online (Sandbox Code Playgroud)

和格式化程序:

formatCell : function (sValue) {
    var a = this; //this references the controller
    return sValue;
}
Run Code Online (Sandbox Code Playgroud)

有人知道如何在 XMLViews 中实现此功能吗?

hir*_*rse 5

在单独的文件中定义格式化程序函数。然后this将是其属性正在被格式化的控件。

我的/自己的/Formatter.js:

sap.ui.define(function () {
    "use strict";
    return {
        formatCell: function (iValue) {
            var idText = this.getId(); //this references the current control
            return iValue;
        }
    };
});
Run Code Online (Sandbox Code Playgroud)

看法:

<Table items="{/rows}">
    <columns>
        <Column>
            <Text text="Some Integer" />
        </Column>
    </columns>
    <items>
        <ColumnListItem>
            <cells>
                <Text text="{ path : 'someInteger', formatter : 'my.own.Formatter.formatCell' }" />
            </cells>
        </ColumnListItem>
    </items>
</Table>
Run Code Online (Sandbox Code Playgroud)

  • 我无法让它工作,在指南中它说要在控制器中包含格式化程序并在控制器中设置``formatter:Formatter``。之后,我可以在 xml 视图中使用 .formatter 来调用格式化程序函数。但它具有视图范围,而不是元素的范围。知道为什么吗? (4认同)