Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
821 views
in Technique[技术] by (71.8m points)

sapui5 - How to pass control reference to formatter in XMLView?

In SAPUI5's JSView, it is quite easy to pass the current control reference to a formatter function:

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

(The 'easy' part of course is because this is referenced in the control's inner formatter function)

However, with XMLViews I haven't managed yet to get a reference to the current control in the formatter function:

<Table items="{/rows}">
    <columns>
        <Column>
            <Text text="Some Integer" />
        </Column>
    </columns>
    <items>
        <ColumnListItem>
            <cells>
                <Text text="{ path : 'someInteger', formatter : '.formatCell' }" />
            </cells>
        </ColumnListItem>
    </items>
</Table>

And the formatter:

formatCell : function (sValue) {
    var a = this; //this references the controller
    return sValue;
}

Anyone knows how to make this work in XMLViews?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Define your formatter functions in a separate file. Then this will be the Control whose property is being formatted.

my/own/Formatter.js:

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

View:

<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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...