Using a Formatter for the Currencies in SAPUI5

孤者浪人 提交于 2020-04-21 18:58:57

问题


I want to build my own formattor for displaying the amount as it should in the different currencies.

One could guess I use this solution I already know:

                                               <t:template>
                                <Text text="{
                                                parts: [
                                                    {path: 'amount'},
                                                    {path: 'currency'}
                                                ],
                                                type:'sap.ui.model.type.Currency',
                                                formatOptions: {
                                                    currencyCode: false
                                                }
                                            }"
             </t:template>

the problem with this solution is I already show the currency in a seperate column and if I go with this solution it looks pretty ugly....

so I tried this one:

<t:template>
                                <Text text="{parts: [                
                                {path: 'amount'},
                                {path: 'currency'}
                                ],               
                                formatter : '.formatter.currency'}"
                                 />
                            </t:template>

and my formatter function looks like this:

    currency: function(amount, currency) {

        var change = [];
        change.push(amount);
        change.push(currency);
        var sInternalType = "";
        var amount1 = new sap.ui.model.type.Currency();


            amount1.formatValue(change, sInternalType);
            return amount1;
    }

Here I would guess I do something completely wrong, as English is not my first language I would probably assume I didnt understood the API References right, as they is stated this:

  • formatValue(vValue, sInternalType): any
  • Format the given array containing amount and currency code to an output value of type string. Other internal types than 'string' are not supported by the Currency type. If an source format is has been defined for this type, the formatValue does also accept a string value as input, which will be parsed into an array using the source format. If aValues is not defined or null, null will be returned.
  • Parameters:
  • {array|string} vValue the array of values or string value to be formatted
  • {string} sInternalType the target type
  • Returns:
  • {any} the formatted output value

回答1:


If it is your intention not to show the currency symbol or code because you already show it elsewhere, you could simply set showMeasure to false, e.g.:

<Text xmlns:core="sap.ui.core"
  core:require="{ CurrencyType: 'sap/ui/model/type/Currency' }"
  text="{
    parts: [
      'amount',
      'currency'
    ],
    type: 'CurrencyType',
    formatOptions: {
      showMeasure: false
    }
  }"
/>

Not showing the currency code/symbol is a feature of the standard Currency type. You don't need to extend it.


Note: In case of OData V4, require the type sap/ui/model/odata/type/Currency instead.




回答2:


If you want to use an custom formatter, you can define it in this way:

currency: function(amount, currency) {

    var oCurrency = new sap.ui.model.type.Currency({
        showMeasure: false       
    });

    return oCurrency.formatValue([amount,curreny], "string");

}

But I would recommend to use the solution from jpenninkhof for your use case



来源:https://stackoverflow.com/questions/38049398/using-a-formatter-for-the-currencies-in-sapui5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!