jqgrid货币格式化程序

时间:2012-04-20 07:40:08

标签: jqgrid

在货币格式化程序的Jqgrid中,只有千位分离器可用,但我想要lakhsSeparator

colModel: [
            {name: 'Code', index: 'Code', width: 55, editable: true, sortable: true },
        { name: 'Ammount', index: 'Ammount', width: 100, editable: true, sortable: false, formatter: 'currency', formatoptions: { prefix: '($', suffix: ')', thousandsSeparator: ','} },
          ],

这里代替成千上万的Separator我想要lakhsSeparator。

2 个答案:

答案 0 :(得分:7)

我觉得这个问题非常有趣。我建议不要实现Globalize插件。 Herehere您可以找到有关它的其他信息。

用法很简单。应该定义使用Globalize.format和使用Globalize.parseFloat函数的custom formatterunformatter。例如

formatter: function (v) {
    // uses "c" for currency formatter and "n" for numbers
    return Globalize.format(Number(v), "c");
},
unformat: function (v) {
    return Globalize.parseFloat(v);
}

为了更加舒适,我建议您定义numberTemplatecurrencyTemplate,例如

var numberTemplate = {align: 'right', sorttype: 'number', editable: true,
        searchoptions: { sopt: ['eq', 'ne', 'lt', 'le', 'gt', 'ge', 'nu', 'nn', 'in', 'ni']},
        formatter: function (v) {
            return Globalize.format(Number(v), "n");
        },
        unformat: function (v) {
            return Globalize.parseFloat(v);
        }},
    currencyTemplate = {align: 'right', sorttype: 'number', editable: true,
        searchoptions: { sopt: ['eq', 'ne', 'lt', 'le', 'gt', 'ge', 'nu', 'nn', 'in', 'ni']},
        formatter: function (v) {
            return Globalize.format(Number(v), "c");
        },
        unformat: function (v) {
            return Globalize.parseFloat(v);
        }};

并在colModel中使用

{ name: 'amount', index: 'amount', width: 150, template: currencyTemplate },
{ name: 'age', index: 'age', width: 52, template: numberTemplate },

The demo使用“en-IN”语言环境并显示如下图所示的结果

enter image description here

答案 1 :(得分:2)

您可以将此功能添加到格式化程序的货币中。首先,您需要修改内置的NumberFormat函数。为此,您可以在加载jqGrid脚本文件后运行以下脚本:

$.fmatter.util.NumberFormat = function(nData,opts) {
    if(!$.fmatter.isNumber(nData)) {
        nData *= 1;
    }
    if($.fmatter.isNumber(nData)) {
        var bNegative = (nData < 0);
        var sOutput = nData + "";
        var sDecimalSeparator = (opts.decimalSeparator) ? opts.decimalSeparator : ".";
        var nDotIndex;
        if($.fmatter.isNumber(opts.decimalPlaces)) {
            var nDecimalPlaces = opts.decimalPlaces;
            var nDecimal = Math.pow(10, nDecimalPlaces);
            sOutput = Math.round(nData*nDecimal)/nDecimal + "";
            nDotIndex = sOutput.lastIndexOf(".");
            if(nDecimalPlaces > 0) {
                if(nDotIndex < 0) {
                    sOutput += sDecimalSeparator;
                    nDotIndex = sOutput.length-1;
                }
                else if(sDecimalSeparator !== "."){
                    sOutput = sOutput.replace(".",sDecimalSeparator);
                }
                while((sOutput.length - 1 - nDotIndex) < nDecimalPlaces) {
                    sOutput += "0";
                }
            }
        }
        if(opts.thousandsSeparator) {
            var sThousandsSeparator = opts.thousandsSeparator;
            nDotIndex = sOutput.lastIndexOf(sDecimalSeparator);
            nDotIndex = (nDotIndex > -1) ? nDotIndex : sOutput.length;
            var sNewOutput = sOutput.substring(nDotIndex);
            var nCount = -1;
            for (var i=nDotIndex; i>0; i--) {
                nCount++;
                if ((nCount%3 === 0) && (i !== nDotIndex) && (!bNegative || (i > 1))) {
                    sNewOutput = sThousandsSeparator + sNewOutput;
                }
                sNewOutput = sOutput.charAt(i-1) + sNewOutput;
            }
            sOutput = sNewOutput;
        }
        else if(opts.lakhsSeparator) {
            var sLakhsSeparator = opts.lakhsSeparator;
            nDotIndex = sOutput.lastIndexOf(sDecimalSeparator);
            nDotIndex = (nDotIndex > -1) ? nDotIndex : sOutput.length;
            var sNewOutput = sOutput.substring(nDotIndex);
            var nCount = -1;
            var i = nDotIndex;
            while (i > 0) {
                for (var nCount = 0; nCount < 7 && i > 0; nCount++) {
                    sNewOutput = sOutput.charAt(i-1) + sNewOutput;
                    if (((nCount === 2) || (nCount === 4) || (nCount == 6)) && (!bNegative || (i > 1))) {
                        sNewOutput = sLakhsSeparator + sNewOutput;
                    }
                    i--;
                }
            }
            sOutput = sNewOutput;
        }
        sOutput = (opts.prefix) ? opts.prefix + sOutput : sOutput;
        sOutput = (opts.suffix) ? sOutput + opts.suffix : sOutput;
        return sOutput;

    } else {
        return nData;
    }
};

现在您可以像这样定义格式化选项:

colModel: [
        { name: 'Code', index: 'Code', width: 55, editable: true, sortable: true },
        { name: 'Ammount', index: 'Ammount', width: 100, editable: true, sortable: false, formatter: 'currency', formatoptions: { prefix: '($', suffix: ')', thousandsSeparator: null, lakhsSeparator: ',' } },
        ...
],

这应该会给你所需的结果。