当 loadonce 选项设置为 false 时,jqGrid 导出到 excel 的问题

问题描述 投票:0回答:1

我有一个 ASP.net MVC 解决方案并在其中使用 jqGrid。为了获得更好的性能,我使用

loadonce: false
作为选项,它应该是这样的,不幸的是它似乎不受 jqGrid 支持,因为我在整个搜索过程中找不到任何迹象。

$(document).ready(function () {
    $("#jqGrid").jqGrid(
        {
            url: "/Student/GetStudents",
            mtype: "GET",
            datatype: "json",
            contentType: "application/json; charset-utf-8",

            jsonReader: {
                root: "rows",
                id: "StudentId",
                repeatitems: false
            },
            colNames: ['StudentId', 'FirstName', 'LastName'],
            colModel: [
                { label: 'StudentId', name: 'Id', key: true, width: 75 },
                { label: 'FirstName', name: 'FirstName', width: 150 },
                { label: 'LastName', name: 'LastName', width: 150 },

            ],
            viewrecords: true,
            loadonce: false,
            width: '100%',
            height: 'auto',
            rowNum: 20,
            rowList: [20, 30, 50],
            sortable: true,
            sortname: 'Id',
            pager: "#jqGridPager",

            autoencode: true,
            scroll: false,
            pgbuttons: true,
            autowidth: true,
            shrinkToFit: false,
            forceFit: false,
            gridview: false,
            height: '100%',
            scrollrows: true,
            page: 1,
            //pagerpos: 'center',
            toppager: true,
            recordpos: 'right',
            multiselect: true,
            multiboxonly: true,
            direction: 'rtl',
            ignoreCase: true,
            caption: "",
            rownumbers: true
        });
    $('#jqGrid').jqGrid('navGrid', '#jqGridPager', {
        search: true,
        searchtext: "Search",
        edit: false,
        add: false,
        del: false,
        excel: true,
        refresh: false,

    }, {}, {}, {}, {
        closeOnEscape: true,
        closeAfterSearch: true,
        ignoreCase: true,
        multipleSearch: false,
        multipleGroup: false,
        showQuery: false,
        sopt: ['cn', 'eq', 'ne'],
        defaultSearch: 'cn'
    })
    $('#jqGrid').jqGrid('navButtonAdd', '#jqGridPager', {
        caption: "Export to Excel",
        //buttonicon: "ui-icon-disk",
        buttonicon: "ui-icon-folder-open",
        onClickButton: function () {
            exportToExcel();
        },

    });
});
function exportToExcel(data, e) {
    exportExcelFile(data);
}


function exportExcelFile() {
    debugger;

    var data = $('#jqGrid')[0].addLocalData(true);
    var ua = window.navigator.userAgent;
    var msie = ua.indexOf("MSIE");
    if (msie > 0 || !!navigator.userAgent.match(/Trident.*rv\:11\./)) {
        frame1.document.open("txt/html", "replace");
        frame1.document.write(setTableOfData(data));
        frame1.document.close();
        frame1.focus();
        sa = frame1.document.execCommand("SaveAs", true, "text.xls");
    } else
        $('#jqGrid').jqGrid('exportToExcel', { fileName: "exportedExcel.xls", navigator: true });
}

function setTableOfData(data) {
    var htmlString = '<table>';
    var header = '<tr><td>StudentId</td><td>FirstName</td><td>LastName</td></tr>';
    htmlString += header;
    for (var i = 0; i < data.length; i++) {
        var tag = '<tr><td>' + data[i].Id + '</td><td>' + data[i].FirstName + '</td><td>' + data[i].LastName + '</td></tr>';
        htmlString += tag;
    }
    htmlString += '</table>';
    return htmlString;
}
excel pagination jqgrid export-to-excel
1个回答
0
投票

最后,我强制将网格的所有过滤和其他设置发布到服务器,并返回一个链接给客户端。然后通过给定的链接我可以下载 Excel 文件。 信息:您无法使用 post(ajax) 请求下载文件。

© www.soinside.com 2019 - 2024. All rights reserved.