javascript - SAPUI5 Use formatter inside formatter - Stack Overflow

i am trying to access a formatter-method from another formatter-method, like this:sap.ui.define(["

i am trying to access a formatter-method from another formatter-method, like this:

sap.ui.define(["<package>/model/formatter"], function(formatter) {
"use strict";

return {
    formatter: formatter,

    roundNumberToTwoDecimals: function(number) {
        if (number) {
            return number.toFixed(2);
        }
    },

    stringFormatter: function(sI18n, dNumber, sProduct) {
        var i18n = this.getModel("i18n");
        if (i18n) {
            return i18n.getText(sI18n, [formatter.roundNumberToTwoDecimals(dNumber), sProduct]);
        }
    }
};

However, my formatter (formatter.roundNumberToTwoDecimals) is undefined.
Does anyone know a solution to this?

Thanks.

i am trying to access a formatter-method from another formatter-method, like this:

sap.ui.define(["<package>/model/formatter"], function(formatter) {
"use strict";

return {
    formatter: formatter,

    roundNumberToTwoDecimals: function(number) {
        if (number) {
            return number.toFixed(2);
        }
    },

    stringFormatter: function(sI18n, dNumber, sProduct) {
        var i18n = this.getModel("i18n");
        if (i18n) {
            return i18n.getText(sI18n, [formatter.roundNumberToTwoDecimals(dNumber), sProduct]);
        }
    }
};

However, my formatter (formatter.roundNumberToTwoDecimals) is undefined.
Does anyone know a solution to this?

Thanks.

Share edited Jan 8, 2021 at 21:06 Sandra Rossi 13.8k6 gold badges25 silver badges56 bronze badges asked Oct 22, 2015 at 7:30 www40www40 2852 gold badges7 silver badges19 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 3

You can define helper functions in private scope.

sap.ui.define(["<package>/model/formatter"], function() {
"use strict";

//private scope
var roundToDecimal = function(iNumber,iFixed){
   return iNumber.toFixed(iFixed);
}

return { 
    roundNumberToTwoDecimals: function(number) {
        if (number) {
            return roundToDecimal(number,2);
        }
    },

    stringFormatter: function(sI18n, dNumber, sProduct) {
        var i18n = this.getModel("i18n");
        if (i18n) {
            return i18n.getText(sI18n, [roundToDecimal(dNumber,2), sProduct]);
        }
    }
};

The function roundToDecimal can be accessed only by the functions within the formatter. It can't be accessed directly as a formatter function from a view as it's not supposed to be exposed as a formatter function but just a helper function. With this way, the context of 'this' passed to the formatter doesn't matter as it changes from jsview to xml view.

this.formatter.roundNumberToTwoDecimals(dNumber);

works fine (this is the controller of the view)

try this.roundNumberToTwoDecimals(dNumber)

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745067953a4609369.html

相关推荐

  • javascript - SAPUI5 Use formatter inside formatter - Stack Overflow

    i am trying to access a formatter-method from another formatter-method, like this:sap.ui.define(["

    3小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信