[英]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:在这里,我想我做错了什么,因为英语不是我的第一语言,我可能会认为我没有正确理解 API 参考,因为它们是这样说的:
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:如果您打算不显示货币符号或代码,因为您已经在其他地方显示了它,您可以简单地将
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
}
}"
/>
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.注意:在 OData V4 的情况下,需要类型
sap/ui/model /odata /type/Currency
。
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但我建议在您的用例中使用 jpenninkhof 的解决方案
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.