简体   繁体   English

从jqgrid将过滤的数据导出到CSV

[英]Export filtered data to CSV from jqgrid

I am using free jqgrid 4.14 and I need to export the data of the grid into CSV/Excel. 我正在使用免费的jqgrid 4.14,我需要将网格数据导出到CSV / Excel。 I have already made a function which exports all the data to CSV. 我已经做了一个将所有数据导出到CSV的功能。 I am capturing the data coming from the server and using that data to form the CSV. 我正在捕获来自服务器的数据,并使用该数据形成CSV。 But here the problem is how will I get only the filtered data from the grid. 但是这里的问题是我将如何仅从网格中获取过滤后的数据。 Is there any default property or function for that? 是否有任何默认属性或函数?

This is my function - 这是我的职能-

  function convertor (gridData, scopes, ShowLabel, fileTitle,grid_header_column_value,id) {


        scopes.grid_header_columns = scopes.grid_header_column_value[id];
        var bg = "<button class='btnfilter' />";
        for (var i = 0; i < scopes.grid_hidden_columns[id].length; i++) {
            if (includes(scopes.grid_header_columns,scopes.grid_hidden_columns[id][i]) == true) {
                var indexhed = scopes.grid_header_columns.indexOf(scopes.grid_hidden_columns[id][i]);
                scopes.grid_header_columns.splice(indexhed, 1);
            }
        }
        var arrData = typeof gridData != 'object' ? JSON.parse(gridData) : gridData;
        var CSV = '';
        if (ShowLabel) {
            var row = "";
            var count = 0;
            if (fileTitle != "" )
            {
                var htmltext = fileTitle.split(',');
                for (var jj = 0; jj < htmltext.length; jj++)
                {
                    var datacont = document.getElementById(htmltext[jj]);
                    if (datacont == undefined)
                        datacont = '';
                    else
                        datacont = datacont.textContent + '\r\n';
                    CSV += datacont;
                }
            }
            for (var index in arrData[0]) {
                var a = scopes.gridextra.indexOf(index);
                if (a == -1 && scopes.grid_header_columns[count] != undefined) {
                    row += scopes.grid_header_columns[count].replace(bg, "") + ',';
                    count++;
                }
            }
            row = row.slice(0, -1);
            CSV += row + '\r\n';
        }

        for (var i = 0; i < arrData.length; i++) {

            var row = "";
            for (var index in arrData[i]) {

                var a = scopes.gridextra.indexOf(index);

                if (a == -1) {
                    if (isNaN(arrData[i][index]) == false) {
                            if(arrData[i][index]<0) {
                                if(arrData[i][index] == -2)
                                    arrValue = '="'+"*"+'"';
                                else
                                    arrValue = '="'+"N/A"+'"';
                            }
                            else
                                var arrValue = arrData[i][index] == null ? "" : '="' + arrData[i][index] + '"';
                    }
                    else
                            var arrValue = arrData[i][index] == null ? "" : '="' + arrData[i][index] + '"';
                        row += arrValue + ',';
                }
            }
            row.slice(0, row.length - 1);
            CSV += row + '\r\n';
        }
        if (CSV == '') {
            growl.error("Invalid data");
            return;
        }
        csvData = CSV;       
    }

If I correctly understand your problem, then you should to use lastSelectedData parameter instead of data parameter. 如果我正确理解了您的问题,则应使用lastSelectedData参数而不是data参数。 The data parameter contains original data. data参数包含原始数据。 The lastSelectedData parameter contains the filtered and sorted data (all pages). lastSelectedData参数包含经过过滤和排序的数据(所有页面)。 See the demos, which I created for the answer . 请参阅为答案创建的演示。

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

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