1

我正在使用 ExtJS 4.1,并尝试将我的货币字段显示为 value / 1000,但仅显示。例如,如果用户输入 1234.56,它将在屏幕上显示为 1,234.56,但仅当它显示在屏幕上时。对于其他所有内容,它都存储为 1234560。在该数字后面的每个计算中都被视为 1234560。我使用 bigint 作为存储,我想避免浮动,因为在我的国家,20 亿是一个正常数字,但也会需要分数部分。

你怎么能那样做?

谢谢你。

4

2 回答 2

1

一种方法是创建一个扩展基本文本框的新组件,向其添加一个名为 storedValue 之类的属性,并为焦点和模糊事件注册处理程序以将存储的值转换为十进制值以进行显示/编辑,然后逗号格式的版本并使用整数值更新存储的值。

编辑

刚回去工作,认为这段旧代码可能有用。这是我不久前为自己创建的货币字段。父表单上的侦听器仅适用于具有更新前后事件的表单扩展版本。可能有更好的方法来做到这一点,例如根据应用程序的需要重载 getValue、getSubmitValue 和 getSubmitData 函数。我的需要只是显示货币符号和逗号,因此需要根据您的需要对其进行修改,但如果您还不太远或遇到任何麻烦,它应该提供一个不错的起点。祝你好运。

Ext.define('Ext.ux.form.field.Currency', {
    extend: 'Ext.form.field.Text',

    alias: 'widget.currencyfield',

    initComponent: function (config) {
        this.callParent(arguments);
    },

    hasFocus: false,

    listeners: {
        render: function () {
            var form = this.findParentByType('form');

            form.on('afterLoadRecord', function () {
                this.toRaw();
                if (this.getRawValue() == 0) {
                    this.setRawValue('');
                } else {
                    this.toFormatted();
                }
            }, this);

            form.on('beforeUpdateRecord', function () {
                this.toRaw();
            }, this);

            form.on('afterUpdateRecord', function () {
                this.toRaw();
                if (this.getRawValue() == 0) {
                    this.setRawValue('');
                } else {
                    this.toFormatted();
                }
            }, this);
        },
        focus: function (field, e, eOpts) {
            this.toRaw();
            this.hasFocus = true;
        },
        blur: function (field, e, eOpts) {
            //Clear out commas and $
            this.toRaw();

            //If there's a value, format it
            if(field.getValue() != '') {
                this.toFormatted();
                this.hasFocus = false;
            }
        }
    },

    stripAlpha: function (value) {
        return value.replace(/[^0-9.]/g, '');
    },

    toRaw: function () {
        if (this.readOnly !== true) {
            this.setRawValue(this.stripAlpha(this.getRawValue()));
        }
    },

    toFormatted: function () {
        this.setRawValue(Ext.util.Format.currency(this.getRawValue(), '$ ', 0));
    },

    getValue: function () {
        return parseFloat(this.stripAlpha(this.getRawValue()));
    }
});
于 2013-01-02T06:08:50.240 回答
0

玩了之后,我有了另一种解决方案,一个基于模型的解决方案。创建一个不存在的字段,比如price_display,用它来展示;

Ext.define('app.model.PriceSample', {
  extend: 'Ext.data.Model',
  field: [
    {name: 'price', type: 'int'},
    {name: 'price_display', type: 'float', convert: function (value, records) {
      if (value) { //on write
        record.set('price', value * 1000);
        return value;
      } else {
        return record.get('price') / 1000;
      }
    }}
  ]
}

在您的网格或组合或任何东西上使用 price_display 而不是价格。但是当你想进行数学运算时,请使用价格。

于 2013-01-03T16:55:32.417 回答