Javascript ExtJS 将 NumberField 扩展为 CurrencyField

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5118111/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 15:44:42  来源:igfitidea点击:

ExtJS extending NumberField to CurrencyField

javascriptextjscurrency

提问by bensiu

There is a very nice Answer how to enforce decimal precision in NumberField: How do I force the display of a decimal in an ExtJS NumberField to a certain precision?.

有一个非常好的答案如何强制执行小数精度NumberField如何强制将 ExtJS NumberField 中的小数显示为特定精度?.

But it only works half way. I would like to do 1 step more and implement a thousands separator and dollar sign (US Money). I tried to extend baseCharsto '1234567890$,' but it did not work.

但它只工作了一半。我想做多一步并实现千位分隔符和美元符号(美国货币)。我试图扩展baseChars到“1234567890$”,但没有成功。

Is someone have this already solved or know how to do it?

有人已经解决了这个问题或知道如何去做吗?

回答by brittongr

i just developed yesterday the extension that you are looking for. I needed this kind of features too, and now was the time to finish this.

我昨天刚刚开发了您正在寻找的扩展程序。我也需要这种功能,现在是完成它的时候了。

You can read the post in this blog and download the source there: http://develtech.wordpress.com/2011/03/06/number-field-with-currency-symbol-thousand-separator-with-international-support

您可以阅读此博客中的帖子并在此处下载源代码:http: //develtech.wordpress.com/2011/03/06/number-field-with-currency-symbol-thousand-separator-with-international-support

or

或者

Visit this thread in the extjs forum http://www.sencha.com/forum/showthread.php?125937-Number-field-with-currency-symbol-thousand-separator-with-international-support&p=577701#post577701

在 extjs 论坛中访问此线程http://www.sencha.com/forum/showthread.php?125937-Number-field-with-currency-symbol-thousand-separator-with-international-support&p=577701#post577701

Hope it helps.

希望能帮助到你。



Code:

代码:

    /**
 * Copyright(c) 2011
 *
 * Licensed under the terms of the Open Source LGPL 3.0
 * http://www.gnu.org/licenses/lgpl.html
 * @author Greivin Britton, [email protected]
 *     
 * @changes
 * No currency symbol by default    
 * No decimalPrecision in config
 * Supporting any character as thousand separator
 * Improved getFormattedValue
 * Removed unnecessary code to create format template, now using float.toFixed(this.decimalPrecission)    
 */

Ext.ux.NumericField = function(config){
    var defaultConfig = 
    {
        style: 'text-align:right;'
    };

    Ext.ux.NumericField.superclass.constructor.call(this, Ext.apply(defaultConfig, config));

    //Only if thousandSeparator doesn't exists is assigned when using decimalSeparator as the same as thousandSeparator
    if(this.useThousandSeparator && this.decimalSeparator == ',' && Ext.isEmpty(config.thousandSeparator))
        this.thousandSeparator = '.';
    else
        if(this.allowDecimals && this.thousandSeparator == '.' && Ext.isEmpty(config.decimalSeparator))
            this.decimalSeparator = ',';

    this.onFocus = this.onFocus.createSequence(this.onFocus);
};

Ext.extend(Ext.ux.NumericField, Ext.form.NumberField, 
{
    currencySymbol: null,
    useThousandSeparator: true,
    thousandSeparator: ',',
    alwaysDisplayDecimals: false,
    setValue: function(v){
       Ext.ux.NumericField.superclass.setValue.call(this, v);

       this.setRawValue(this.getFormattedValue(this.getValue()));
    },
    /**
     * No more using Ext.util.Format.number, Ext.util.Format.number in ExtJS versions
     * less thant 4.0 doesn't allow to use a different thousand separator than "," or "."
     * @param {Number} v
     */
    getFormattedValue: function(v){

        if (Ext.isEmpty(v) || !this.hasFormat()) 
            return v;
        else 
        {
            var neg = null;

            v = (neg = v < 0) ? v * -1 : v; 
            v = this.allowDecimals && this.alwaysDisplayDecimals ? v.toFixed(this.decimalPrecision) : v;

            if(this.useThousandSeparator)
            {
                if(this.useThousandSeparator && Ext.isEmpty(this.thousandSeparator))
                    throw ('NumberFormatException: invalid thousandSeparator, property must has a valid character.');

                if(this.thousandSeparator == this.decimalSeparator)
                    throw ('NumberFormatException: invalid thousandSeparator, thousand separator must be different from decimalSeparator.');

                var v = String(v);

                var ps = v.split('.');
                ps[1] = ps[1] ? ps[1] : null;

                var whole = ps[0];

                var r = /(\d+)(\d{3})/;

                var ts = this.thousandSeparator;

                while (r.test(whole)) 
                    whole = whole.replace(r, '' + ts + '');

                v = whole + (ps[1] ? this.decimalSeparator + ps[1] : '');
            }

            return String.format('{0}{1}{2}', (neg ? '-' : ''), (Ext.isEmpty(this.currencySymbol) ? '' : this.currencySymbol + ' '), v);
        }
    },
    /**
     * overrides parseValue to remove the format applied by this class
     */
    parseValue: function(v){
        //Replace the currency symbol and thousand separator
        return Ext.ux.NumericField.superclass.parseValue.call(this, this.removeFormat(v));
    },
    /**
     * Remove only the format added by this class to let the superclass validate with it's rules.
     * @param {Object} v
     */
    removeFormat: function(v){
        if (Ext.isEmpty(v) || !this.hasFormat()) 
            return v;
        else 
        {
            v = v.replace(this.currencySymbol + ' ', '');

            v = this.useThousandSeparator ? v.replace(new RegExp('[' + this.thousandSeparator + ']', 'g'), '') : v;
            //v = this.allowDecimals && this.decimalPrecision > 0 ? v.replace(this.decimalSeparator, '.') : v;

            return v;
        }
    },
    /**
     * Remove the format before validating the the value.
     * @param {Number} v
     */
    getErrors: function(v){
        return Ext.ux.NumericField.superclass.getErrors.call(this, this.removeFormat(v));
    },
    hasFormat: function()
    {
        return this.decimalSeparator != '.' || this.useThousandSeparator == true || !Ext.isEmpty(this.currencySymbol) || this.alwaysDisplayDecimals;    
    },
    /**
     * Display the numeric value with the fixed decimal precision and without the format using the setRawValue, don't need to do a setValue because we don't want a double
     * formatting and process of the value because beforeBlur perform a getRawValue and then a setValue.
     */
    onFocus: function(){
        this.setRawValue(this.removeFormat(this.getRawValue()));
    }
});
Ext.reg('numericfield', Ext.ux.NumericField);

回答by Rob Boerman

If I understand you correctly, you want to enforce double precission and add a dollar sign. I think it is best adding a renderer to your field:

如果我理解正确,您想强制执行双精度并添加美元符号。我认为最好在您的字段中添加渲染器:

{
    ...
    fieldLabel: 'amount',
    renderer: MyApp.dollarRenderer
}

MyApp.dollarRenderer = function(amount) {
    return "$ "+sprintf('%.02f',amount);
}

-> 100.5 becomes $ 100.50

The renderer itself would need a sprintf function (which for god know why is still not natively implemented in Javascript. There is however a nice one I use: http://www.diveintojavascript.com/projects/javascript-sprintf

渲染器本身需要一个 sprintf 函数(天知道为什么它仍然没有在 Javascript 中本地实现。但是我使用了一个很好的函数:http: //www.diveintojavascript.com/projects/javascript-sprintf

Adding a thousand separator, change the renderer with this niceregexp:

添加千位分隔符,使用这个漂亮的正则表达式更改渲染器:

MyApp.dollarRenderer = function(amount) {
    return "$ "+sprintf('%.02f',amount).replace(/\d{1,3}(?=(\d{3})+(?!\d))/g,'$&,');
}
MyApp.dollarRenderer(1298129827.4) -> "$ 1,298,129,827.40"

Good luck

祝你好运

回答by Frederic THOMAS

/**
 * @author: Frédéric Thomas
 * Date: 22/03/12
 * Time: 16:37
 */
Ext.define('yourNS.form.field.Currency', {
    extend: 'Ext.form.field.Number',
    alias: ['widget.currencyField'],
    config: {
        thousandSeparator: ' ',
        currencyAtEnd: true,
        currencySign: ''
    },

    listeners: {
        /**
         * When this component get the focus, change the Currency
         * representation to a Float one for edition.
         *
         * @param me
         * @param eOpts
         */
        focus: function (me, eOpts) {
            me.inputEl.dom.value = this.getValue();
        }
    },

    /**
     * Converts a Float value into a currency formated value ready to display .
     *
     * @param {Object} value
     * @return {Object} The converted value.
     */
    valueToCurrency: function (value) {
        var format = Ext.util.Format;
        format.currencyPrecision = this.decimalPrecision;
        format.thousandSeparator = this.thousandSeparator;
        format.currencySign = this.currencySign;
        format.currencyAtEnd = true;
        return format.currency(value);
    },

    /**
     * Converts a mixed-type value to a raw representation suitable for displaying in the field. This allows controlling
     * how value objects passed to {@link #setValue} are shown to the user, including localization.
     *
     * See {@link #rawToValue} for the opposite conversion.
     *
     * This implementation converts the raw value to a value formated as currency.
     *
     * @param {Object} value The mixed-type value to convert to the raw representation.
     * @return {Object} The converted raw value.
     */
    valueToRaw: function (value) {
        return this.valueToCurrency(value);
    },

    /**
     * Performs any necessary manipulation of a raw String value to prepare it for conversion and/or
     * {@link #validate validation}. Overrided to apply the {@link #parseValue}
     * to the raw value.
     *
     * @param {String} value The unprocessed string value
     * @return {String} The processed string value
     */
    processRawValue: function (value) {
        return this.parseValue(this.callParent(arguments));
    },

    /**
     * Overrided to remove thousand separator.
     *
     * @param value
     */
    parseValue: function (value) {
        value = String(value).replace(this.thousandSeparator, "");
        value = parseFloat(String(value).replace(this.decimalSeparator, '.'));
        return isNaN(value) ? null : value;
    }
});

回答by JamesHalsall

A similar component has been done here for US Dollars:

此处为美元完成了类似的组件:

http://www.sencha.com/forum/showthread.php?90184-Currency-Ext.form.NumberField-and-similar.../page2

http://www.sencha.com/forum/showthread.php?90184-Currency-Ext.form.NumberField-and-similar.../page2

It's far from perfect though.

虽然它远非完美。