开发者

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

开发者 https://www.devze.com 2023-03-05 18:40 出处:网络
i have an asp.net-mvc web page and i am using jqgrid on the front end.i want to 开发者_StackOverflow中文版have an export to excel button that will export the current set of data (based on the current

i have an asp.net-mvc web page and i am using jqgrid on the front end. i want to 开发者_StackOverflow中文版have an export to excel button that will export the current set of data (based on the current filter).

i already use the toolbar filter so i see that the filter settings are stored in post data but i can't figure out how to create a method that will pass along all of the filter settings / rules to the server from jqgrid.

i see a bunch of jqgrid "export to excel" example after googling but similar to this example, none of them seem to be passing the filter information to the serverside.


You could put hidden fields inside the Export to Excel form:

@using (Html.BeginForm(new { action = "ExportToExcel" }))
{
    @Html.Hidden("sidx")
    @Html.Hidden("sord")
    @Html.Hidden("page")
    @Html.Hidden("rows")
    <table id="list"></table>
    <input type="submit" value="Export to Excel" />
}

and populate them upon form submission based on the current values:

$('form').submit(function () {
    var grid = $('#list');
    var sortname = grid.getGridParam('sortname');
    var sortorder = grid.getGridParam('sortorder');
    var page = grid.getGridParam('page');
    var rows = grid.getGridParam('rowNum');
    $('#sidx').val(sortname);
    $('#sord').val(sortorder);
    $('#page').val(page);
    $('#rows').val(rows);
});

This way the ExportToExcel controller action will take those parameters and be able to filter the list.


What I have done is put the gridstate into the cache each time data is requested, then I do the export to excel using the gridState. There are examples of this somewhere on the jqGrid site:

//this fragment in GetData method
Session["ExceptionGridState"] = gridModel.ExceptionGrid.GetState(true);

Then when the export is called:

public ActionResult ExportToExcel_CurrentData()
{
  var gridModel = new ExceptionJqGridModel();
  var grid = gridModel.ExceptionGrid;

  // call the ExportToExcel built-in method
  JQGridState gridState = Session["ExceptionGridState"] as JQGridState;
  gridState.CurrentPageOnly = false;
  grid.ExportToExcel(SourceQuery(),
        String.Format("SomeDatasetName_Filtered_{0:yyyymmddhhmm}.xls", 
            DateTime.Now), 
        gridState);

return View();

}

This works for me.


I succeed to make filtered export, taking inspiration by above @simpatric greg solution.

I set one session variable for each grid parameter, when data is requested and then passing again them to the excel export service. Greg's solution can work with asp.net MVC, which is ok for the main question. The following solution could be used with standard pure js jqgrid too:

CONTROLLER GRID ACTION

        ...
      Session["jqsidx"] = sidx; 
      Session["jqsord"] = sord; 
      Session["jqpage"] = page; 
      Session["jqrows"] = rows; 
      Session["jq_search"] = _search; 
      Session["jqfilters"] = filters; 
      ....

RECALLED INSIDE OF EXCEL EXPORT ACTION ^^

 string sidx = Session["jqsidx"] as String;
 string sord = Session["jqsord"] as String;
 int? page = Session["jqpage"] as Nullable<Int32>;
 int? rows = Session["jqrows"] as Nullable<Int32>;
 bool? _search = Session["jq_search"] as Nullable<bool>;
 string filters = Session["jqfilters"] as String;

var query = myqueryservice.getGridData(sidx, sord, (int)page, (int)rows, (bool)_search, filters, urlparams).ToList();
...

I hope this may help for other people having the same problem with standard jqgrid.

0

精彩评论

暂无评论...
验证码 换一张
取 消