Export jqgrid filtered data as excel or CSV

China☆狼群 提交于 2020-01-09 19:40:53

问题


I am in trouble please help me out.I want to show "export to excel" button in the pager of jqgrid, that will export the current set of data which is retrieve after searching criteria of jqgrid (based on the current filter). I am using "loadonce:true" setting for my jqgrid.Now I want to export data from local datasource of jqgrid after searching. If it is not possible then how I can able to pass parameters to a server when I click on export button of navigation on which searching criteria need to do. I am using back-end as a servlet.


回答1:


I would recommend you to implement export of data on the server and just post the current searching filter to the back-end. Full information about the searching parameter defines postData parameter of jqGrid. Another boolean parameter of jqGrid search define whether the searching filter should be applied of not. You should better ignore _search property of postData parameter and use search parameter of jqGrid.

Format of the information about the searching filter depend on whether use used multipleSearch: true option. I personally use it always. In the case the full information about the filter will be placed in one property of the postData parameter: filters property. The format is described here. The code which gets the information looks like the following

var $grid = $("#list"),
    isFilterAreUsed = $grid.jqGrid('getGridParam', 'search'),
    filters = $grid.jqGrid('getGridParam', 'postData').filters;

If you don't use multipleSearch: true option you will be not able to use complex filters and the information about the filter will be placed in three properties of the postData parameter: searchField, searchOper and searchString.




回答2:


According to Oleg answer it is possible to use

javascript code:

$("#grid").jqGrid('navButtonAdd', '#grid_toppager', {
        caption: "Excel",
        buttonicon: "ui-icon-save",
        onClickButton: function () {
            document.forms['_export']._buffer.value = $("#grid").jqGrid('getGridParam', 'postData');
            document.forms['_export'].submit();
        }
    });

Index.aspx:

<form  id='_export' method="post" action='<%= Url.Action( "Export", "Grid", new { _entity= Model.Name } ) %>'>
    <input type="hidden" name="_buffer" id="_buffer" value="" />
    </form>

Controller:

public ActionResult Export(string _entity, string _sidx, string _sord, string filters ) {
            string where = "";
            if (!string.IsNullOrEmpty(filters))
            {
                var serializer = new JavaScriptSerializer();
                Filters filtersList = serializer.Deserialize<Filters>(filters);
                where = filtersList.FilterObjectSet(entity);
            }
            if (string.IsNullOrEmpty(where))
                where = " TRUE ";
            Response.ClearContent();
            Response.AddHeader("content-disposition", "attachment; filename=MyExcelFile.xls");
            Response.ContentType = "application/excel";
            Response.Write(GetAllData(_entity, _sidx, _sord, where));
            Response.End();
            return View("Index");
}

filters parameter in controller has null value. No idea Which is proper way to pass filters and sorting parameters to Export method.



来源:https://stackoverflow.com/questions/8227898/export-jqgrid-filtered-data-as-excel-or-csv

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!