在 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 参考文献,因为他们是这样说的:

如果您不想显示货币符号或代码,因为您已经在别处显示了它,您可以简单地将 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
    }
  }"
/>

不显示货币 code/symbol 是标准货币类型的一项功能。您不需要扩展它。


注意:如果是 OData V4,则需要类型 sap/ui/model<strong>/odata</strong>/type/Currency

如果你想使用自定义格式化程序,你可以这样定义它:

currency: function(amount, currency) {

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

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

}

但我建议您将 jpenninkhof 的解决方案用于您的用例