如何强制ExtJS NumberField中的小数显示达到一定的精度?

时间:2009-08-17 12:04:28

标签: extjs extjs4 extjs3

我有一个NumberField的表单,它从JSON获取类型为float的值。如果值恰好是整数,则不显示小数位。我想一直显示2位小数。是否有配置选项?

这是我的声明:

items: [
    { 
        fieldLabel: 'Net Sales',
        name: 'netSales',
        allowBlank:false,
        decimalPrecision:2
    },

7 个答案:

答案 0 :(得分:14)

由于这是关于在NumberField中强制小数的搜索查询的最高结果,我想我会为那些使用ExtJS 4 +的人更新这个

自ExtJS 4委托给valueToRaw函数以来的输入过滤,所使用的setValue函数实际上来自Ext.form.field.Text,所以这就是我在下面重写的内容。

我还决定强制显示小数是一个选项('forcePrecision')可配置每个NumberField,这意味着覆盖将如下所示:

Ext.override(Ext.form.NumberField, {
    forcePrecision : false,

    valueToRaw: function(value) {
        var me = this,
            decimalSeparator = me.decimalSeparator;
        value = me.parseValue(value);
        value = me.fixPrecision(value);
        value = Ext.isNumber(value) ? value : parseFloat(String(value).replace(decimalSeparator, '.'));
        if (isNaN(value))
        {
          value = '';
        } else {
          value = me.forcePrecision ? value.toFixed(me.decimalPrecision) : parseFloat(value);
          value = String(value).replace(".", decimalSeparator);
        }
        return value;
    }
});

要在表单中使用它,您可以像这样实例化它:

{
  xtype: 'numberfield',
  name:  'decimalsfield',
  forcePrecision: true,     #defaults to false
  decimalPrecision: 3       #defaults to 2
}

未使用 forcePrecision:true 实例化的字段与默认字符串完全相同。

答案 1 :(得分:11)

流行的解决方案对我不起作用。事实上,解决方案中的代码与EXT JS 3.3源代码完全相同,对于我来说,当decimalPrecision为2时,显示“1”而不是“1.00”。根据流行解决方案的建议来覆盖setValue,这就是我做了:

Ext.override(Ext.form.NumberField, {
    setValue: function(v) {
        var dp = this.decimalPrecision;
        if (dp < 0 || !this.allowDecimals) {
            dp = 0;
        }
        v = this.fixPrecision(v);
        v = Ext.isNumber(v) ? v : parseFloat(String(v).replace(this.decimalSeparator, "."));
        v = isNaN(v) ? '' : String(v.toFixed(dp)).replace(".", this.decimalSeparator);
        return Ext.form.NumberField.superclass.setValue.call(this, v);
    }
});

即使fixPrecision代码似乎用所需的精度格式化数字,javascript也会在调用超类的setValue函数之前在两行上操作时失去精度。

当最终转换为字符串时,使用toFixed设置精度。

祝你好运!

答案 2 :(得分:9)

延伸

var myNumberField = Ext.extend(Ext.form.NumberField, {
        setValue : function(v){
            v = typeof v == 'number' ? v : String(v).replace(this.decimalSeparator, ".");
            v = isNaN(v) ? '' : String(v).replace(".", this.decimalSeparator);
            //  if you want to ensure that the values being set on the field is also forced to the required number of decimal places.
            // (not extensively tested)
            // v = isNaN(v) ? '' : this.fixPrecision(String(v).replace(".", this.decimalSeparator));
            return Ext.form.NumberField.superclass.setValue.call(this, v);
        },
        fixPrecision : function(value){
            var nan = isNaN(value);
            if(!this.allowDecimals || this.decimalPrecision == -1 || nan || !value){
               return nan ? '' : value;
            }
            return parseFloat(value).toFixed(this.decimalPrecision);
        }
    });

...
...

items: [new myNumberField({
        id  : 'net',
        fieldLabel: 'Net Sales',
        allowBlank:false,
        decimalPrecision:2
    }),

覆盖,这将影响您应用中的所有数字字段:

Ext.override(Ext.form.NumberField, {
    setValue : function(v){
            v = typeof v == 'number' ? v : String(v).replace(this.decimalSeparator, ".");
        v = isNaN(v) ? '' : String(v).replace(".", this.decimalSeparator);
        return Ext.form.NumberField.superclass.setValue.call(this, v);
    },
    fixPrecision : function(value){
        var nan = isNaN(value);
        if(!this.allowDecimals || this.decimalPrecision == -1 || nan || !value){
           return nan ? '' : value;
        }
        return parseFloat(value).toFixed(this.decimalPrecision);
    }
})

items: [{
        xtype   : 'numberfield',
        fieldLabel: 'Net Sales',
        allowBlank:false,
        decimalPrecision:2
    },

<强> 修改

注意第一个setValue方法中的注释部分。

答案 3 :(得分:1)

最好的办法是为它添加一个侦听器,然后在字段的值上使用内置的Javascript .toFixed(2)函数。

答案 4 :(得分:0)

选项decimalPrecision定义是: “小数点分隔符后显示的最大精度(默认为2)”

没有强制格式的选项,你可能必须覆盖NumberField类。

答案 5 :(得分:0)

如果您想要以下内容:

输入50&gt;你得到了50岁

50.10&gt; 50.10

50.123&gt; 50.12

试试这个:

, setValue: function (v) {
      if ( !v || isNaN(v)) {
        v = '';
      }
      else if (v % 1 != 0) {    //means number is not whole number
        v = this.fixPrecision(String(v).replace(".", this.decimalSeparator));
      }

     return Ext.form.NumberField.superclass.setValue.call(this, v);
}
, fixPrecision: function (value) {
    if (!this.allowDecimals || this.decimalPrecision == -1) {
       return value;
    }

    return parseFloat(value).toFixed(this.decimalPrecision);
}

答案 6 :(得分:0)

此代码对我很有用。它不会在ExtJS 4.2.0中使用“ValueToRow”覆盖。

var myNumberField = Ext.extend(Ext.form.NumberField, {
    setValue : function(v){
        v = typeof v == 'number' ? v : String(v).replace(this.decimalSeparator, ".");
        v = isNaN(v) ? '' : String(v).replace(".", this.decimalSeparator);
        return Ext.form.NumberField.superclass.setValue.call(this, v);
    },
    fixPrecision : function(value){
        var nan = isNaN(value);
        if(!this.allowDecimals || this.decimalPrecision == -1 || nan || !value){
        return nan ? '' : value;
        }
        var val = parseFloat(value).toFixed(this.decimalPrecision);
        return val;
    },
    valueToRaw: function(value) {
        var me = this,
        decimalSeparator = me.decimalSeparator;
        value = me.parseValue(value);
        value = me.fixPrecision(value);
        value = Ext.isNumber(value) ? value : parseFloat(String(value).replace(decimalSeparator, '.'));
        value = isNaN(value) ? '' : String(value).replace('.', decimalSeparator);
        return me.fixPrecision(value);
    }
});

...
...
items: [new myNumberField({
        id  : 'net',
        fieldLabel: 'Net Sales',
        allowBlank:false,
        decimalPrecision:2
    })

参考: How to keep the trailing zeros in Ext.form.field.Number ?