自制的EXTJS的货币输入框和百分比输入框

Ext.namespace('Ext.ux');

Ext.ux.CustomNumberField = Ext.extend(Ext.form.NumberField, {
fieldClass: 'x-form-field x-form-customnumber-field',
prefixChar: '',
suffixChar: '',
numberDelim: ',',
delimLength: 3,
alwaysShowCents: true,
onRender: function(){
Ext.ux.CustomNumberField.superclass.onRender.apply(this,arguments);
var name = this.name || this.el.dom.name;
this.hiddenField = this.el.insertSibling({
tag:'input'
,type:'hidden'
,name:name
,value:this.parseValue(this.value)
});
this.hiddenName = name;
this.el.dom.removeAttribute('name');
this.el.on({
keyup:{scope:this, fn:this.updateHidden}
,blur:{scope:this, fn:this.updateHidden}
}, Ext.isIE ? 'after' : 'before');
this.setValue = this.setValue.createSequence(this.updateHidden);
},
initEvents: function() {
Ext.ux.CustomNumberField.superclass.initEvents.call(this);
var allowed = this.baseChars + '';
var stripBeforeParse = [];

if (this.allowDecimals) {
allowed += this.decimalSeparator;
}
if (this.allowNegative) {
allowed += '-';
} if (this.prefixChar) {
allowed += this.prefixChar;
stripBeforeParse.push(Ext.escapeRe(this.prefixChar));
} if (this.suffixChar) {
allowed += this.suffixChar;
stripBeforeParse.push(Ext.escapeRe(this.suffixChar));
} if (this.numberDelim) {
allowed += this.numberDelim;
stripBeforeParse.push(Ext.escapeRe(this.numberDelim));
}
this.maskRe = new RegExp('[' + Ext.escapeRe(allowed) + ']');
this.stripBeforeParseRe = new RegExp('[' + stripBeforeParse.join('|') + ']','g');
},
updateHidden:function() {
this.hiddenField.dom.value = this.parseValue(this.getValue());
},    
getErrors: function() {
var errors = Ext.form.NumberField.superclass.getErrors.apply(this, arguments);        
return errors;
},

setValue: function(v) {
v = this.formatValue(this.parseValue(v));
Ext.form.NumberField.superclass.setValue.call(this,v);
},

parseValue: function(value) {
value = String(value).replace(this.stripBeforeParseRe, '');
value = Ext.ux.CustomNumberField.superclass.parseValue.call(this, value);
return value;
},

formatValue: function(value) {
if(Ext.isEmpty(value)) return '';
value = String(Ext.ux.CustomNumberField.superclass.fixPrecision.call(this, value));
var vSplit = value.split('.');

var cents = (vSplit[1]) ? '.' + vSplit[1] : '';
if (this.alwaysShowCents && cents == '') cents = '.00';

if (this.numberDelim && this.delimLength) {
var numbers = vSplit[0].split('');
var sNumbers = [];
var c=0;
while (numbers.length > 0) {
c++;
if (c > this.delimLength) c = 1;
sNumbers.unshift(numbers.pop());
if (c == this.delimLength && numbers.length > 0) sNumbers.unshift(this.numberDelim);
}
value = sNumbers.join('') + cents;
} else {
value = vSplit[0] + cents;
}
if (this.prefixChar) value = this.prefixChar + String(value);
if (this.suffixChar) value = String(value) + this.suffixChar;
return value;
}
});

Ext.reg('custnumberfield',Ext.ux.CustomNumberField);

Ext.ux.MoneyField = Ext.extend(Ext.ux.CustomNumberField, {
currencyChar: '¥',
initComponent: function(){
Ext.apply(this,{
prefixChar: this.currencyChar  
});
Ext.ux.MoneyField.superclass.initComponent.apply(this,arguments);
}
});

Ext.reg('moneyfield', Ext.ux.MoneyField);

Ext.ux.PercentField = Ext.extend(Ext.ux.CustomNumberField, {
percentChar: '%',
initComponent: function(){
Ext.apply(this,{
suffixChar: this.percentChar  
});
Ext.ux.PercentField.superclass.initComponent.apply(this,arguments);
}
});

Ext.reg('percentfield', Ext.ux.PercentField); 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值