简体   繁体   中英

JqGrid: Choose columns for export with ExcelExport?

I'm using jqgrid to show the contents of a database filled with (university) course evaluations. It consists of about 20-25 columns and 30k rows.

The purpose of the webtool (jqgrid) is to be able to filter the data and export selected columns/rows to excel and to diagrams. So far I've managed to get the ExcelExport working (I'm using PHPExcel to actually create the xlsx-file though but not really relevant), however the data sent to the server doesn't include anything about which columns are visible - it does include the filters though so that's all good. I'm using column chooser and filtertoolbar to show selected data.

Basically I want to send the colModel along with the filters sent to excelexport (and later on to google charts as well). I found this way of getting the colModel:

var colModel = $("#list").jqGrid('getGridParam','colModel');

This is how the excelexport function looks in the jqgrid source:

    excelExport : function(o) {
        o = $.extend({
            exptype : "remote",
            url : null,
            oper: "oper",
            tag: "excel",
            exportOptions : {}
        }, o || {});
        return this.each(function(){
            if(!this.grid) { return;}
            var url;
            if(o.exptype === "remote") {
                var pdata = $.extend({},this.p.postData);
                pdata[o.oper] = o.tag;
                var params = jQuery.param(pdata);
                if(o.url.indexOf("?") !== -1) { url = o.url+"&"+params; }
                else { url = o.url+"?"+params; }
                window.location = url;
            }
        });

I've tried editing this but with no results (is it not possible to edit this function?) so then I though I would make my own ajax-call but I don't really know where to begin. How can I access the postData when writing my own call? Or is there a better way?

Thank you!

Finally got it working, sharing the solution in case someone else runs into the same problem.

First of all - ajax can't be used to produce a file in this manner so that's not the way to go.

What I ended up doing was using a function very similar to the one in the source and adding the visible columns as a param "columns".

Add a button to the pager:

 $grid.jqGrid('navButtonAdd', '#pager',
                    {caption: '',
                        title: 'Export to Excel',
                        onClickButton: function(e) {

Create an array of the visible columns:

                            var cols = [];
                            var mycolModel = $("#list").getGridParam("colModel");
                            $.each(mycolModel, function(i) {
                                if (!this.hidden) {
                                    cols.push(this.name);
                                }
                            });

Add the array to the params together with the params that jqgrid was gonna send anyway (like filter, search, sidx etc):

                            var pdata = $grid.jqGrid('getGridParam', 'postData');
                            var colsJ = JSON.stringify(cols);
                            var params = jQuery.param(pdata);
                            params = params + "&columns=" + colsJ;

Send the params to your excelexporter:

                            var url = 'ExcelExport.php' + "?" + params;
                            window.location = url;
                        }
                    });
        });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM