【问题标题】:Using a Formatter for the Currencies in SAPUI5在 SAPUI5 中使用货币格式化程序
【发布时间】:2016-10-29 04:45:38
【问题描述】:

我想构建自己的格式化程序,以显示应以不同货币显示的金额。

可能会猜到我使用的是我已经知道的解决方案:

                                               <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} 格式化输出值

【问题讨论】:

    标签: javascript sapui5 formatter


    【解决方案1】:

    如果您打算不显示货币符号或代码,因为您已经在其他地方显示了它,您可以简单地将 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<strong>/odata</strong>/type/Currency

    【讨论】:

    • 我将此用于 OData 双向绑定,但在更新价格值时遇到问题。价格(例如命名金额)在我的 Edm:Decimal 的情况下 - 它作为字符串而不是数字发送。不幸的是,货币类型正在发送一个浮点值。有没有办法让它正常工作?
    • @user3783327 应该可以使用附加格式选项parseAsString: true。默认是false
    【解决方案2】:

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

    currency: function(amount, currency) {
    
        var oCurrency = new sap.ui.model.type.Currency({
            showMeasure: false       
        });
    
        return oCurrency.formatValue([amount,curreny], "string");
    
    }
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-10-18
      • 2011-09-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多