gpt4 book ai didi

javascript - ExtJS 将 NumberField 扩展为 CurrencyField

转载 作者:数据小太阳 更新时间:2023-10-29 04:32:55 36 4
gpt4 key购买 nike

如何在 NumberField 中执行小数精度有一个非常好的答案:How do I force the display of a decimal in an ExtJS NumberField to a certain precision?

但它只起到了一半的作用。我想多做 1 步并实现千位分隔符和美元符号(美国货币)。我尝试将 baseChars 扩展为“1234567890$”,但没有成功。

是否有人已经解决了这个问题或知道如何去做?

最佳答案

我昨天刚刚开发了您正在寻找的扩展。我也需要这种功能,现在是时候完成它了。

您可以阅读此博客中的帖子并在那里下载源代码:http://develtech.wordpress.com/2011/03/06/number-field-with-currency-symbol-thousand-separator-with-international-support

访问 extjs 论坛中的这个主题 http://www.sencha.com/forum/showthread.php?125937-Number-field-with-currency-symbol-thousand-separator-with-international-support&p=577701#post577701

希望对您有所帮助。


代码:

    /**
* Copyright(c) 2011
*
* Licensed under the terms of the Open Source LGPL 3.0
* http://www.gnu.org/licenses/lgpl.html
* @author Greivin Britton, brittongr@gmail.com
*
* @changes
* No currency symbol by default
* No decimalPrecision in config
* Supporting any character as thousand separator
* Improved getFormattedValue
* Removed unnecessary code to create format template, now using float.toFixed(this.decimalPrecission)
*/

Ext.ux.NumericField = function(config){
var defaultConfig =
{
style: 'text-align:right;'
};

Ext.ux.NumericField.superclass.constructor.call(this, Ext.apply(defaultConfig, config));

//Only if thousandSeparator doesn't exists is assigned when using decimalSeparator as the same as thousandSeparator
if(this.useThousandSeparator && this.decimalSeparator == ',' && Ext.isEmpty(config.thousandSeparator))
this.thousandSeparator = '.';
else
if(this.allowDecimals && this.thousandSeparator == '.' && Ext.isEmpty(config.decimalSeparator))
this.decimalSeparator = ',';

this.onFocus = this.onFocus.createSequence(this.onFocus);
};

Ext.extend(Ext.ux.NumericField, Ext.form.NumberField,
{
currencySymbol: null,
useThousandSeparator: true,
thousandSeparator: ',',
alwaysDisplayDecimals: false,
setValue: function(v){
Ext.ux.NumericField.superclass.setValue.call(this, v);

this.setRawValue(this.getFormattedValue(this.getValue()));
},
/**
* No more using Ext.util.Format.number, Ext.util.Format.number in ExtJS versions
* less thant 4.0 doesn't allow to use a different thousand separator than "," or "."
* @param {Number} v
*/
getFormattedValue: function(v){

if (Ext.isEmpty(v) || !this.hasFormat())
return v;
else
{
var neg = null;

v = (neg = v < 0) ? v * -1 : v;
v = this.allowDecimals && this.alwaysDisplayDecimals ? v.toFixed(this.decimalPrecision) : v;

if(this.useThousandSeparator)
{
if(this.useThousandSeparator && Ext.isEmpty(this.thousandSeparator))
throw ('NumberFormatException: invalid thousandSeparator, property must has a valid character.');

if(this.thousandSeparator == this.decimalSeparator)
throw ('NumberFormatException: invalid thousandSeparator, thousand separator must be different from decimalSeparator.');

var v = String(v);

var ps = v.split('.');
ps[1] = ps[1] ? ps[1] : null;

var whole = ps[0];

var r = /(\d+)(\d{3})/;

var ts = this.thousandSeparator;

while (r.test(whole))
whole = whole.replace(r, '$1' + ts + '$2');

v = whole + (ps[1] ? this.decimalSeparator + ps[1] : '');
}

return String.format('{0}{1}{2}', (neg ? '-' : ''), (Ext.isEmpty(this.currencySymbol) ? '' : this.currencySymbol + ' '), v);
}
},
/**
* overrides parseValue to remove the format applied by this class
*/
parseValue: function(v){
//Replace the currency symbol and thousand separator
return Ext.ux.NumericField.superclass.parseValue.call(this, this.removeFormat(v));
},
/**
* Remove only the format added by this class to let the superclass validate with it's rules.
* @param {Object} v
*/
removeFormat: function(v){
if (Ext.isEmpty(v) || !this.hasFormat())
return v;
else
{
v = v.replace(this.currencySymbol + ' ', '');

v = this.useThousandSeparator ? v.replace(new RegExp('[' + this.thousandSeparator + ']', 'g'), '') : v;
//v = this.allowDecimals && this.decimalPrecision > 0 ? v.replace(this.decimalSeparator, '.') : v;

return v;
}
},
/**
* Remove the format before validating the the value.
* @param {Number} v
*/
getErrors: function(v){
return Ext.ux.NumericField.superclass.getErrors.call(this, this.removeFormat(v));
},
hasFormat: function()
{
return this.decimalSeparator != '.' || this.useThousandSeparator == true || !Ext.isEmpty(this.currencySymbol) || this.alwaysDisplayDecimals;
},
/**
* Display the numeric value with the fixed decimal precision and without the format using the setRawValue, don't need to do a setValue because we don't want a double
* formatting and process of the value because beforeBlur perform a getRawValue and then a setValue.
*/
onFocus: function(){
this.setRawValue(this.removeFormat(this.getRawValue()));
}
});
Ext.reg('numericfield', Ext.ux.NumericField);

关于javascript - ExtJS 将 NumberField 扩展为 CurrencyField,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5118111/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com