4

In Jqgrid for currency formatter there is only thousandsSeparator is available but i want 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: ','} },
          ],

here in place of thousandsSeparator i want lakhsSeparator.

4

2 回答 2

7
于 2012-04-20T11:24:36.057 回答
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: ',' } },
        ...
],

那应该会给您所需的结果。

于 2012-04-20T09:40:07.103 回答