简体   繁体   中英

Using a Formatter for the Currencies in SAPUI5

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

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 , eg:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM