代码之家  ›  专栏  ›  技术社区  ›  Mike Sickler

如何强制将extjs numberfield中的小数显示到一定精度?

  •  14
  • Mike Sickler  · 技术社区  · 15 年前

    我有一张表格 NumberField 获取类型的值 float 来自JSON。如果值恰好是整数,则不显示小数点。我想一直显示两位小数。这个有配置选项吗?

    这是我的声明:

    items: [
        { 
            fieldLabel: 'Net Sales',
            name: 'netSales',
            allowBlank:false,
            decimalPrecision:2
        },
    
    7 回复  |  直到 11 年前
        1
  •  8
  •   Joshua    15 年前

    任何一个 延伸 :

    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方法中的注释部分。

        2
  •  14
  •   Dave A    12 年前

    因为这是一个关于在数字字段中强制小数的搜索查询的首要结果,所以我想我会为使用extjs 4的用户更新这个结果。+

    自从extjs 4被委托给valuetoraw函数以来的输入过滤,使用的setvalue函数实际上来自ext.form.field.text,所以下面我将覆盖这个函数。

    我还决定强制显示小数作为每个数字字段可配置的选项(“forcePrecision”),这意味着覆盖将如下所示:

    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
    }
    

    字段未用实例化 力精度:真 行为与默认设置完全相同。

        3
  •  11
  •   Jens    14 年前

    流行的解决方案对我不起作用。实际上,解决方案中的代码是extJS3.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代码似乎以所需的精度格式化了数字,但在调用超类的setvalue函数之前,javascript会在两行操作期间丢失精度。当最终转换为字符串时,使用tofixed设置精度,使其工作正常。

    祝你好运!

        4
  •  1
  •   Jason    15 年前

    最佳选择可能是为模糊事件添加一个侦听器,然后对字段值使用内置的javascript.tofixed(2)函数。

        5
  •  0
  •   Davide Ungari    15 年前

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

    没有强制格式化的选项,您可能必须重写NumberField类。

        6
  •  0
  •   Umang    11 年前

    如果您需要如下内容:

    输入50>u得到50

    50.10以上;50.10

    50.123以上;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);
    }
    
        7
  •  0
  •   Nuwa    11 年前

    这个代码对我很有用。它不适用于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 ?