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.
2 Answers
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
.

- 334,321
- 69
- 703
- 674

- 220,925
- 34
- 403
- 798
-
Hi Oleg, I have done as you specify and It's working now. I have placed your code in **onClickButton** event of my custom navigation button and after getting filter elements I am calling servlet method by ajax which gives excel file. Thanks for your reply,It saves my lot's of time. – Bhagwat Gurle Nov 23 '11 at 06:00
-
@Bhagwat: I'm glad to know that I could help you. You are welcome! – Oleg Nov 23 '11 at 06:28
-
@Oleg: I tried your answer but filters are not passed. I updated Bhagwat question and added testcase – Andrus Nov 23 '11 at 09:58
-
1@Andrus: What you mean under "filters are not passed"? I described only how one can *get* the current `filters` which are used and don't how to pass the information to somewhere. Additionally I don't see that the Bhagwat`s question are changed and I can't see any testcase. You should explain more clear what you mean. – Oleg Nov 23 '11 at 10:21
-
@Oleg: It looks like by edit is not published, maybe wait for moderaton. I added answer describing the issue. – Andrus Nov 23 '11 at 13:03
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.

- 26,339
- 60
- 204
- 378
-
1Why you don't just use jQuery.ajax to post the data to the server? Do you verified with `alert` that `$("#grid").jqGrid('getGridParam', 'postData')` get you the information which you need? – Oleg Nov 23 '11 at 13:18
-
@Oleg: I need to open excel result in separate tab or prompt user to download xls file. Ajax does not support this.`alert($("#grid").jqGrid('getGridParam', 'postData').filters)` returns `{"groupOp":"AND","rules":[{"field":"Klient_nimi","op":"cn","data":"emil"}]} ` – Andrus Nov 23 '11 at 13:41
-
1I personally use in the cases direct setting of `window.location` (see [here](http://stackoverflow.com/q/6581791/315935) or [here](http://stackoverflow.com/q/5759889/315935)). For example `window.location = myServerBaseUrl/Export + '?filters=' + encodeURIComponent(filters);` inside of `onClickButton`. – Oleg Nov 23 '11 at 13:48
-
Thank you, it worked. If Export controller throws excepton it returns error message as json content. Browses shows 'Not found' page in this case. User doesnt see error message. – Andrus Nov 23 '11 at 14:46
-
1In [the answer](http://stackoverflow.com/q/5501644/315935) I showed how one can define class `HandleJsonExceptionAttribute` used as the replacement of `[HandleError]` attribute. You can place `Export` action in another controller and use HTML and not json content in the case. You can also provide HTML content on error directly in the `Export` action. So I think that you should just change the error reporting in the `Export` action. – Oleg Nov 23 '11 at 15:43