Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
836 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 Answer

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...