jqGrid Export to CSV - Post Rather than Get

前端 未结 2 1898
野的像风
野的像风 2020-12-22 00:50

I have a jqGrid that uses Post to send the request. I have a php function that when given the jqGrid search and sort settings can return a CSV file. And, I have put togeth

2条回答
  •  不思量自难忘°
    2020-12-22 01:39

    Amy, I had the same problem. I am not a programer (you will realize that by the code I will paste here :) ) but the solution I've found seems to work fine.

    This a solution I had for another similar problem (not related to jqGrid).

    excelExport : function(o) { 
        o = $.extend({
            exptype : "remote",
            url : null,
            oper: "oper",
            tag: "excel",
            exportOptions : {}
        }, o || {});
        return this.each(function(){
            if(!this.grid) { return;}
            if(o.exptype == "remote") {
                var pdata = $.extend({},this.p.postData);
    
                pdata[o.oper] = o.tag;
    
                var form = document.createElement("form");
                form.setAttribute("method", "post");
                form.setAttribute("action", o.url);
                form.setAttribute("target", "_blank");
    
                $.each( pdata, function(i, l){
                    if (typeof l != 'undefined') {
                        if (typeof l == 'function') {
                            post_value = l();
                        }
                        else {
                            post_value = l;
                        }
                        var hiddenField = document.createElement("input");
                        hiddenField.setAttribute("type", "hidden");
                        hiddenField.setAttribute("name", i);
                        hiddenField.setAttribute("value", post_value);
                        form.appendChild(hiddenField);
                    }
                 });
    
                document.body.appendChild(form);    // Not entirely sure if this is necessary
                form.submit();
    
            }
        });
    }
    

    As you can see it creates a form and posts the data to a new page. Most people here would find a better (and more elegant) way to do this but this solution, as is, works. I need to send a lot of information to the server so a GET is not enough for me, that is why I needed to POST the data.

    Hope this works for you.

    JMG.

提交回复
热议问题