ext 扩展控件—moneyField

时间:2023-03-08 18:03:13
ext 扩展控件—moneyField
/**
*数字控件
*带大写提示,和千分位
**/
Ext.define(appNameSpace+'.utils.MoneyField', {
extend : 'Ext.form.field.Text',
alias : 'widget.moneyfield',
initComponent : function() {
var me = this;
Ext.apply(Ext.form.field.VTypes, {
regexMoney: function(val, field) {
val = val.replace(/,/g,'');
if(isNaN(Number(val))){
if(field.negative_Text && val.indexOf("-")== 0 && val.replace(/-/g,"").length == val.length-1){
return true
}else{
return false;
}
}else{
return true;
}
},
regexMoneyText: "请输入数字,最大保留两位小数!"
});
me.on('keypress',function(t,e){
if(t.value.indexOf(".") != -1 && e.keyCode == 46){
e.stopEvent();
}
if((!t.negative_Text) && (44>e.keyCode && e.keyCode>58)){
e.stopEvent();
}else if((t.negative_Text) && (45>e.keyCode && e.keyCode>58)){
e.stopEvent();
} });
me.on("mouseover",function(){console.log("123")})
// Ext.apply(me,{});
this.callParent(arguments); // 调用你模块的initComponent函数
},
baseChars: '0123456789.-',
maskRe :new RegExp('[-0-9\.]'),
fieldStyle:"text-align:right;",
enableKeyEvents:true,
cursor:null,
vtype:"regexMoney",
getTip: function() {
var tip = this.tip;
if (!tip) {
tip = this.tip = Ext.widget('tooltip', {
target: this.el,
width: 420,
autoHide: false,
anchor: 'top',
//closable: true,
mouseOffset: [100, -2],
constrainPosition: false
});
tip.show();
}
return tip;
},
getValue: function(flag) {
var me = this,
val = me.rawToValue(me.processRawValue(me.getRawValue()));
// me.value = val;
if(!flag){
val = val.replace(/,/g,'');
}
return val;
},
onChange:function(newVal, oldVal){
/*if(value.replace(/,/g,'') == oldVal.replace(/,/g,'')){
if(newVal.length < oldVal.length){
for(var i=0;i<oldVal.length;i++){
if(newVal[i] != oldVal[i]){
this.setValue(this.toThousands(value));
}
}
}else{
return;
}
}*/
var t = this;
if(isNaN(Number(newVal.replace(/,/g,'')))){
if(t.negative_Text && newVal.indexOf("-")==0 && newVal.replace(/-/g,"").length == newVal.length-1){
return
}else{
this.setValue("");
return;
}
}
if(!t.isValid()){
return;
}
var el = this.inputEl.dom;
/**数字大写**/
if(newVal==oldVal){
return;
}
var decimal = "";
var value = newVal;
/*if(newVal.indexOf(".") != -1){
if(newVal.substring(value.indexOf(".")+1).indexOf(".") != -1){
this.setValue(oldVal);
return;
}
}*/
if(newVal.indexOf(",") != -1){
value = newVal.replace(/,/g,'');
if(newVal.replace(/,/g,'') == oldVal.replace(/,/g,'')){
return;
}
} var tip = this.getTip();
t.setValue(t.getValue(true).replace(/\s/g, ""));
t.setValue(t.getValue(true).replace(/。/g, "."));
if(t.value != null && t.value != ''){
tip.setDisabled(false);
tip.setTitle(atoc(value));
tip.show();
}else{
tip.setDisabled(true);
tip.hide();
}
/**千分位**/
if(newVal == ""){
return;
} if(value.indexOf(".") != -1){
var valLength = value.substring(value.indexOf(".")).length;
if(valLength == 1){
decimal = ".";
}else{
decimal = value.substring(value.indexOf("."));
}
value = value.substring("0",value.indexOf("."));
}
var n_ = "";
if(this.negative_Text && value.indexOf("-")==0){
value = value.replace(/-/g,'');
n_ = "-"
}
this.setValue(n_ + this.toThousands(value,newVal)+decimal);
el.setSelectionRange(this.cursor,this.cursor);
return;
},
onBlur:function(e){
var me = this;
if(!me.isValid()){
return;
}
var val = me.getValue(true);
/**数字大写**/
var tip = this.getTip();
tip.setDisabled(true);
tip.hide();
/**小数位**/
var value = val.replace(/,/g,'');
if(val.indexOf(".") != -1){
value = parseFloat(value).toFixed(2);
var sVal = val.substring(0,val.indexOf("."));
var eVal = value.substring(value.indexOf("."));
var n_ = "";
if(this.negative_Text && value.indexOf("-")==0){
value = value.replace(/-/g,'');
n_ = "-"
}
me.setValue(n_ + this.toThousands(sVal)+eVal);
}else{
if(val != ""){
me.setValue(this.toThousands(value));
}
}
return;
},
onFocus:function(e){
var me = this;
var el = this.inputEl.dom;
el.setAttribute("onpaste","return false");
var val = me.getValue(true);
var newVal = val;
/**数字大写**/
var t = this;
if(!t.isValid()){
return;
}
var tip = this.getTip();
t.newVal = t.value;
t.setValue(t.getValue(true).replace(/\s/g, ""));
t.setValue(t.getValue(true).replace(/。/g, "."));
if(t.value != null && t.value != ''){
tip.setDisabled(false);
tip.setTitle(atoc(newVal));
tip.show();
}else{
tip.setDisabled(true);
tip.hide();
}
me.getValue(true);
return;
},
toThousands:function(num,val) {
var el = this.inputEl.dom;
var value = this.value;
if(num.indexOf(",") != -1){
num = num.replace(/,/g,'');
}
var str = el.selectionStart;
var num = (num || 0).toString(), result = '';
while (num.length > 3) {
result = ',' + num.slice(-3) + result;
num = num.slice(0, num.length - 3);
}
if (num) { result = num + result; }
//光标在最后的时候
if(str == value.length && value.indexOf(".")==-1){
this.cursor = result.length;
}else if((result.length - value.length == 1 && value.indexOf(".")==-1)||(result.length - value.substring(0,value.indexOf(".")).length==1 && value.indexOf(".") !=-1)){
this.cursor = str+1;
}else if(result.length - value.length == -1 && value.indexOf(".")==-1||(result.length - value.substring(0,value.indexOf(".")).length==-1 && value.indexOf(".") !=-1)){
this.cursor = str-1;
}else if(result.length == value.length && value.indexOf(".")==-1){
this.cursor = str;
}else if(str == value.length && value.indexOf(".") !=-1){
this.cursor = value.length;
}else if(value.indexOf(".") !=-1 ){
this.cursor = str;
}
if(this.negative_Text && value.indexOf("-")==0){
this.cursor = this.cursor+1
}
/*if(str == val.length &&val.indexOf(".") == -1){
this.cursor = result.length;
}else if(str == val.length &&val.indexOf(".") != -1){
var num = val.substring(val.indexOf(".")).length;
this.cursor = result.length + num;
}else if(str == this.value.length){
this.cursor = str;
}*/
return result;
}
});