Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
185 views
in Technique[技术] by (71.8m points)

jquery - Export jqgrid data into excel/pdf with currency format

I need to export jqgrid data with currency format like $4,355.

My colmodel is below. I am calling function like exportGrid('griddtable',[1,2,3,4,5])

                name : 'totalSpend',
                index : 'totalSpend',
                align : 'right',
                sorttype : 'number',
                formatter : 'currency',
                formatoptions : {
                    prefix : '$',
                    thousandsSeparator : ','
                }

function exportGrid(table, filtered) {

var mya = new Array();
mya = $("#" + table).getDataIDs(); // Get All IDs
var data = $("#" + table).getRowData(mya[0]); // Get First row to get the
// labels
var colNames = new Array();
var ii = 0;
for ( var i in data) {
    colNames[ii++] = i;
} // capture col names
var html = "";
for ( var k = 0; k < colNames.length; k++) {
    if (filtered.indexOf(k) >= 0) {
            html = html + colNames[k] + "	";
    }
}
html = html + "
"; // Output header with end of line
for (i = 0; i < mya.length; i++) {
    data = $("#" + table).getRowData(mya[i]); // get each row
    for ( var j = 0; j < colNames.length; j++) {
        if (filtered.indexOf(j) >= 0) {
            html = html + data[colNames[j]] + "	"; // output each Row as
            // tab delimited
        }
    }
    html = html + "
"; // output each row with end of line
}
html = html + "
"; // end of line at the end
var form = "<form name='csvexportform' action='exportData.do?method=excelExport' method='post'>";
form = form + "<input type='hidden' name='fileName' value='" + fileName
        + "'>";
form = form + "<input type='hidden' name='csvBuffer' value='" + html + "'>";
form = form + "</form><script>document.csvexportform.submit();</sc"
        + "ript>";
OpenWindow = window.open('', '');
OpenWindow.document.write(form);
OpenWindow.document.close();

   }

Any suggestions? Is there any possibility to get the row data with formatted value?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Finally I got it. I used some tricks, but it is working great. I identified all money values by using their decimal places and formatted that value. Hence i got $ symbol on all monsy values. I knew it's not an optimal solution. But may be useful for someone.

var RE = new RegExp(/^d*.dd$/);           //Regular expression to find all 2 decimal values
for (i = 0; i < mya.length; i++) {
    data = $("#" + table).getRowData(mya[i]); // get each row
    for ( var j = 0; j < colNames.length; j++) {
        if (filtered.indexOf(j) >= 0) {
           if (RE.test(data[colNames[j]])) {       //If matches add $symbol
               html = html + "$" + data[colNames[j]] + "	"; // output for money each Row as
                // tab delimited
           } else {
               html = html + data[colNames[j]] + "	"; // output each Row as
                // tab delimited
           }
        }
    }
    html = html + "
"; // output each row with end of line
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...