jqgrid,在 asp.net-mvc 站点中导出到 excel(包含当前过滤器发布数据)

发布于 2024-11-07 06:52:58 字数 369 浏览 0 评论 0原文

我有一个 asp.net-mvc 网页,我在前端使用 jqgrid 。我想要一个导出到 Excel 按钮,该按钮将导出当前的数据集(基于当前的过滤器)。

我已经使用工具栏过滤器,所以我看到过滤器设置存储在发布数据中,但我不知道如何创建一个方法,将所有过滤器设置/规则从 jqgrid 传递到服务器。

我在谷歌搜索后看到一堆 jqgrid“导出到 excel”示例,但 与此示例类似 ,他们似乎都没有将过滤器信息传递到服务器端。

i have an asp.net-mvc web page and i am using jqgrid on the front end. i want to 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

杯别 2024-11-14 06:52:58

您可以将隐藏字段放入导出到 Excel 表单中:

@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" />
}

并在表单提交时根据当前值填充它们:

$('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);
});

这样,ExportToExcel 控制器操作将采用这些参数并能够过滤列表。

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.

素食主义者 2024-11-14 06:52:58

我所做的就是每次请求数据时将 gridstate 放入缓存中,然后使用 gridState 导出到 excel。 jqGrid 站点上有这样的示例:

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

然后当调用导出时:

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();

}

这对我有用。

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.

瀟灑尐姊 2024-11-14 06:52:58

我成功地进行了过滤导出,受到上述 @simpatric greg 解决方案的启发。

当请求数据时,我为每个网格参数设置一个会话变量,然后将它们再次传递到 Excel 导出服务。 Greg 的解决方案可以与 asp.net MVC 一起使用,这对于主要问题来说是可以的。以下解决方案也可以与标准纯js jqgrid一起使用:

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();
...

我希望这可能对遇到同样问题的其他人有所帮助使用标准 jqgrid。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文