views:

601

answers:

2

I have just started working on asp.net mvc and jqgrid.

I have a calendar which returns a date, a multiselect list box and apply filter button outside of the grid. Is there a way to pass these filter values to the server-side actionresult GridData() based on the selected date and multiple selected values and also it persist while paging or sorting.

public ActionResult GridData(string sidx, string sord, int? page, int? rows, Collection categoryOptions,string fromDate) {..}

Thanks!

+1  A: 
Oleg
This seems to be very complex can't we try using session
paresh
I you have one table and need to filter of two fields, you are absolutely right. But if this table only a small example to show you problems - I recommend you to try implement search in a table of multi-search. You write one code and use it in all you jqGrids independent on its complexity.Generally I use external filtering mostly for composed filtering like filtering for Summer, Holiday etc instead of searching for one day only. Interesting for you can be "Integrated Search Toolbar" (see. //trirand.com/blog/jqgrid/jqgrid.html "New in version 3.5")
Oleg
External filtering and "Integrated Search Toolbar" is good for a standard user. Every user sees filtering possibility immediately. "Advanced searching" is good for users who ask you to have Excel export in all you grids. If you have a large web application with a lot of grids, users of the application knows the possibilities of the user interface (GUI). Power users like "advanced searching".
Oleg
+2  A: 

Yes you can use the postData property to send additional filter parameters with each request. Note this will only work if you're using JSON to populate your grid. Just have an action that returns JsonResult.

In your jqgrid config include:

postData: {
   startDate: function() { return $('#startDate').val(); },
   anotherFilter: function() { return $('#anotherFilter').val(); }
}

For your apply filter button call $('#GridName').trigger('reloadGrid'). Alternatively I like to reload the grid anytime a filter changes. You can do this with jquery:

$('#filterName').change(function(){$('#GridName').trigger('reloadGrid');})

Your JSON should contain these properties for jqgrid to understand it:

total = pagedList.PageCount,
page = pagedList.PageNumber,
records = pagedList.TotalItemCount,
rows = pagedList.ToArray()
Ryan
Very good idea to use function in postData! I don't know before, that $.param function used in $.ajax supports function type.
Oleg
Setting of filters one should use inside 'change' and 'keyup' events. I mean this, but not write it in my answer. Good one more time!$('#filterName').change(myRefresh).keyup(function (e) { var keyCode = e.keyCode || e.which; if (keyCode === 33 /*page up*/|| keyCode === 34 /*page down*/|| keyCode === 35 /*end*/|| keyCode === 36 /*home*/|| keyCode === 38 /*up arrow*/|| keyCode === 40 /*down arrow*/) { myRefresh(); } });needed also to support keyboard input for select elements.
Oleg
I forgot to mention that I try to keep my JS very standardized so that I can build it using HtmlHelpers. The above code in my system would look like postData: { JqGridPostFilterValues(x => x.StartDate, x => s.AnotherFilter) }
Ryan