jqgrid currency formatter

后端 未结 2 1923
遇见更好的自我
遇见更好的自我 2020-12-17 05:38

In Jqgrid for currency formatter there is only thousandsSeparator is available but i want lakhsSeparator

colModel: [
            {name: \'Code\', index: \'Co         


        
2条回答
  •  一整个雨季
    2020-12-17 05:59

    You can add this functionality to currency the formatter. First you would need to modify the built in NumberFormat function. To do this you can run below script after loading jqGrid script files:

    $.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;
        }
    };
    

    Now you can define your formatting options like this:

    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: ',' } },
            ...
    ],
    

    That should give you the required result.

提交回复
热议问题