Skip to content

Instantly share code, notes, and snippets.

@mdesign83
Created June 20, 2014 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mdesign83/f764e09093ce938e4bee to your computer and use it in GitHub Desktop.
Save mdesign83/f764e09093ce938e4bee to your computer and use it in GitHub Desktop.
ExtJS 4.2.1 Ext.ux.CurrencyField
/**
* ExtJS CurrencyField
* Updated version of http://develtech.wordpress.com/2011/03/06/number-field-with-currency-symbol-thousand-separator-with-international-support/#respond
*
* Settings configurable via default Ext.util.Format properties:
* Ext.util.Format.currencyAtEnd = false;
* Ext.util.Format.currencyPrecision = 2;
* Ext.util.Format.currencySign = '$ ';
* Ext.util.Format.thousandSeparator = ',';
* Ext.util.Format.decimalSeparator = '.';
*/
Ext.define('Ext.ux.CurrencyField', {
extend: 'Ext.form.field.Text',
alias: ['widget.currencyfield','widget.currency'],
maskRe: /[0-9 .,-]/,
fieldStyle: 'text-align: right;',
onFocus: function(){
this.setRawValue(this.removeFormat(this.getRawValue()));
},
onBlur: function(){
if (this.hasFormat()) {
this.setRawValue(Ext.util.Format.currency(this.getRawValue()));
}
},
setValue: function(v){
this.setRawValue(Ext.util.Format.currency(v));
},
removeFormat: function(v){
if (Ext.isEmpty(v) || !this.hasFormat()) {
return v;
} else {
v = v.replace(Ext.util.Format.currencySign, '');
v = !Ext.isEmpty(Ext.util.Format.thousandSeparator) ? v.replace(new RegExp('[' + Ext.util.Format.thousandSeparator + ']', 'g'), '') : v;
v = Ext.isEmpty(Ext.util.Format.decimalSeparator) ? v.replace(new RegExp('[' + Ext.util.Format.decimalSeparator + ']', 'g'), '') : v;
return v;
}
},
hasFormat: function(){
return Ext.util.Format.thousandSeparator != '.' || !Ext.isEmpty(Ext.util.Format.currencySign) || Ext.util.Format.currencyPrecision > 0;
},
processRawValue: function(val) {
return this.removeFormat(val);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment