【问题标题】:Export To Excel filtered data with Free jqgrid 4.15.4 in MVC在 MVC 中使用 Free jqgrid 4.15.4 导出到 Excel 过滤数据
【发布时间】:2018-09-24 13:32:41
【问题描述】:

我有一个关于在 free-jqgrid 4.15.4 中导出到 Excel 的问题。我想知道如何在我的业务逻辑方法中使用这个结果集{"groupOp":"AND","rules":[{"field":"FirstName","op":"eq","data":"Amit"}]}

为了更清楚起见,我使用的是 OfficeOpenXml,如果我不使用过滤结果集(上述),它工作正常,我可以在 Excel 中下载包含完整记录的文件床单。但我不确定该做什么或如何利用结果集{"groupOp":"AND","rules":[{"field":"FirstName","op":"eq","data":"Amit"}]}

如果需要,我可以共享我的控制器和 BL 代码。

我添加了一个小提琴,它显示了 jqGrid 寻呼机中导出到 Excel 按钮的实现。

在来这里之前,我已经阅读并尝试从以下问题中理解:

1]jqgrid, export to excel (with current filter post data) in an asp.net-mvc site

2]Export jqgrid filtered data as excel or CSV

代码如下:

$(function () {
"use strict";
var mydata = [
    { id: "10",  FirstName: "test", LastName: "TNT", Gender: "Male" },
     { id: "11",     FirstName: "test2",    LastName: "ADXC", Gender: "Male" },
     { id: "12",     FirstName: "test3",    LastName: "SDR", Gender: "Female" },
     { id: "13",     FirstName: "test4",    LastName: "234", Gender: "Male" },
     { id: "14",     FirstName: "test5",    LastName: "DAS", Gender: "Male" },
];
$("#list").jqGrid({
data: mydata,

    colNames: ['Id', 'First Name', 'Last Name', 'Gender'],
     colModel: [
        {
            label: "Id",
            name: 'Id',
            hidden: true,
            search: false,
        },
        {
            label: "FirstName",
            name: 'FirstName',
            searchoptions: {
                searchOperators: true,
                sopt: ['eq', 'ne', 'lt', 'le','ni', 'ew', 'en', 'cn', 'nc'],
            }, search: true,
        },
        {
            label: "LastName",
            name: 'LastName',
            searchoptions: {
                searchOperators: true,
                sopt: ['eq', 'ne', 'lt', 'ni', 'ew', 'en', 'cn', 'nc'],
            }, search: true,
        },

        {
            label: "Gender",
            name: 'Gender',
            search: true, edittype: 'select', editoptions: { value: 'Male:Male;Female:Female' }, stype: 'select',

        },
        ],
        onSelectRow: function (id) {
        if (id && id !== lastsel) {
            jQuery('#list').restoreRow(lastsel);
            jQuery('#list').editRow(id, true);
            lastsel = id;
        }
    },
    loadComplete: function (id) {
        if ($('#list').getGridParam('records') === 0) {

            //$('#grid tbody').html("<div style='padding:6px;background:#D8D8D8;'>No records found</div>");
        }
        else {
            var lastsel = 0;
            if (id && id !== lastsel) {
                jQuery('#list').restoreRow(lastsel);
                jQuery('#list').editRow(id, true);
                lastsel = id;
              }
            }
        },      
    loadonce: true,
    viewrecords: true,
    gridview: true,
    width: 'auto',
    height: '150px',    
    emptyrecords: "No records to display",
    iconSet:'fontAwesome',
    pager: true,
    jsonReader:
    {
        root: "rows",
        page: "page",
        total: "total",
        records: "records",
        repeatitems: false,
        Id: "Id"
    },

});
jQuery("#list").jqGrid("navButtonAdd", {
    caption: "",
    buttonicon: "fa-table",
    title: "Export To Excel",
    onClickButton: function (e) {
        var projectId = null;
        var isFilterAreUsed = $('#grid').jqGrid('getGridParam', 'search'),
           filters = $('#grid').jqGrid('getGridParam', 'postData').filters;   
        var Urls = "/UsersView/ExportToExcel_xlsxFormat?filters="+ encodeURIComponent(filters); //' + encodeURIComponent(filters);/
        if (totalRecordsCount > 0) {
            $.ajax({
                url: Urls,
                type: "POST",
                //contentType: "application/json; charset=utf-8",
                data: { "searchcriteria": filters, "projectId": projectId, "PageName": "MajorsView" },
                //datatype: "json",
                success: function (data) {
                    if (true) {
                        window.location = '/UsersView/SentFiletoClientMachine?file=' + data.filename;
                    }
                    else {
                        $("#resultDiv").html(data.errorMessage);
                        $("#resultDiv").addClass("text-danger");
                    }                       
                },
                error: function (ex) {
                    common.handleAjaxError(ex.status);                        
                }
            });
        }
        else {
            bootbox.alert("There are no rows to export in the Participant List")
            if (dialog) {
                dialog.modal('hide');
            }
        }
    }
});
});

https://jsfiddle.net/ap43xecs/10/

【问题讨论】:

    标签: asp.net-mvc-5 jqgrid free-jqgrid


    【解决方案1】:

    有很多选项可以解决这个问题。最简单的一种是将过滤行的 id 发送到服务器,而不是发送 filters 参数。免费 jqGrid 支持lastSelectedData 参数,因此您可以使用$('#grid').jqGrid('getGridParam', 'lastSelectedData') 来获取排序和过滤的项目对应于当前过滤器和排序条件的数组。返回数组的每一项都应包含Id 属性(或id 属性),您可以在服务器端使用该属性在导出前过滤数据。

    第二个选项是根据您当前发送到服务器的filters 参数实现服务器端过滤。 The old answer(请参阅FilterObjectSet)提供了在使用实体框架的情况下进行过滤的示例。顺便说一句,the answeranother one 包含代码,我使用这些代码使用 Open XML SDK 将网格数据导出到 Excel。您可以将其与现有代码进行比较。

    在某些情况下,将网格数据导出到 Excel 可能会很有趣,而无需编写任何服务器代码。相应的演示可以在the issuethe answerUPDATED部分找到。

    【讨论】:

    • 您好@Oleg,如何在将var filters= $('#grid').jqGrid('getGridParam', 'lastSelectedData') 作为ajax 调用中的参数传递时处理超出最大请求长度?喜欢data:{ searchcriteria: JSON.stringify(filters)},
    • @Chandan:我不确定你的意思。 var filters= $('#grid').jqGrid('getGridParam', 'lastSelectedData'); 可用于获取网格的所有本地数据。它不进行 Ajax 调用。我想错误“超出最大请求长度”显示在服务器代码的某些限制问题上,而不是在客户端的任何限制上。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多