簡體   English   中英

在 SAPUI5 中為貨幣使用格式化程序

[英]Using a Formatter for the Currencies in SAPUI5

我想構建自己的格式化程序來顯示不同貨幣的金額。

有人可能猜到我使用了這個我已經知道的解決方案:

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

這個解決方案的問題是我已經在一個單獨的列中顯示了貨幣,如果我使用這個解決方案,它看起來很丑......

所以我嘗試了這個:

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

我的格式化程序功能如下所示:

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

在這里,我想我做錯了什么,因為英語不是我的第一語言,我可能會認為我沒有正確理解 API 參考,因為它們是這樣說的:

  • formatValue(vValue, sInternalType): 任何
  • 將包含金額和貨幣代碼的給定數組格式化為字符串類型的輸出值。 Currency 類型不支持除 'string' 之外的其他內部類型。 如果已為此類型定義了源格式,則 formatValue 也接受字符串值作為輸入,該值將使用源格式解析為數組。 如果 aValues 未定義或為 null,則返回 null。
  • 參數:
  • {array|string} vValue 要格式化的值或字符串值的數組
  • {string} sInternalType 目標類型
  • 返回:
  • {any} 格式化輸出值

如果您打算不顯示貨幣符號或代碼,因為您已經在其他地方顯示了它,您可以簡單地將showMeasure設置為false ,例如:

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

不顯示貨幣代碼/符號是標准貨幣類型的一個特征。 你不需要擴展它。


注意:在 OData V4 的情況下,需要類型sap/ui/model /odata /type/Currency

如果要使用自定義格式化程序,可以這樣定義:

currency: function(amount, currency) {

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

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

}

但我建議在您的用例中使用 jpenninkhof 的解決方案

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM