简体   繁体   English

JqG​​rid:选择要使用ExcelExport导出的列?

[英]JqGrid: Choose columns for export with ExcelExport?

I'm using jqgrid to show the contents of a database filled with (university) course evaluations. 我正在使用jqgrid显示充满(大学)课程评估的数据库的内容。 It consists of about 20-25 columns and 30k rows. 它由大约20-25列和30k行组成。

The purpose of the webtool (jqgrid) is to be able to filter the data and export selected columns/rows to excel and to diagrams. Webtool(jqgrid)的目的是能够过滤数据并将选定的列/行导出为ex​​cel和图表。 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. 到目前为止,我已经设法使ExcelExport正常工作(尽管我使用PHPExcel实际创建了xlsx文件,但并不真正相关),但是发送到服务器的数据不包括可见的列的任何内容-它确实包括过滤器,所以这一切都很好。 I'm using column chooser and filtertoolbar to show selected data. 我正在使用列选择器和filtertoolbar来显示选定的数据。

Basically I want to send the colModel along with the filters sent to excelexport (and later on to google charts as well). 基本上,我想将colModel与发送到excelexport的过滤器一起发送(以后再发送到Google图表)。 I found this way of getting the colModel: 我发现这种获取colModel的方式:

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

This is how the excelexport function looks in the jqgrid source: 这是excelexport函数在jqgrid源代码中的样子:

    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. 我尝试编辑此函数,但是没有结果(是否可以编辑此函数?),所以尽管我会进行自己的Ajax调用,但是我真的不知道从哪里开始。 How can I access the postData when writing my own call? 编写自己的呼叫时如何访问postData? 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. 首先-不能以这种方式使用ajax来生成文件,所以这不是可行的方法。

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): 将数组与jqgrid无论如何都会发送的参数一起添加到参数中(例如过滤器,搜索,sidx等):

                            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: 将参数发送给您的excelexporter:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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