In developing my first ASP.NET MVC 3 app using the jqGrid to display some data, I'm using the column header filters and also allowing for the advanced filter toolbar filtering to be done. Independently these things work pretty well.
First question - Has anyone a solution for communicating the current column header filter settings to the advanced filters?
As an example, a user can filter on the "Ice Cream Name" column, entering a partial name, e.g., "Chocolate", and it'll filter down to "Chocolate Explosion", "Dark Chocolate", etc. - great. What would be nice would be to open the advanced filter and have that "contains 'Chocolate'" column filter automatically populated in the advanced filter. I recognize that the other direction (where someone could AND or OR two values for the same column, e.g. 'Chocolate' OR 'Caramel') becomes problematic but in the other direction, it seems like it might be possible. Perhaps this is just a setting of the grid I'm missing. Anyone solved this?
Second question - I currently can do some filtering with the column header filters, show some result set in the grid and then go into the advanced filter dialog and set up a different filter. That will display the correct results but the column header filters are not cleared, giving the impression that the filtering is not working. How can I reset those column header filters after the use clicks the "Find" button on the dialog?
I find your question very interesting, so I prepared the demo which demonstrate how one can combine Advanced Searching dialog and Toolbar Searching in one grid.
One important, but simple trick is the usage of
recreateFilter: true
. Per default the searching dialog will be created once and then will be only hide or show. As the result thepostData.filters
parameter will be not refreshed. After settingrecreateFilter: true
the problem with filling of the advanced searching dialog with the values from the searching toolbar will be solved. I personally set the default searching options as the followingNow to more complex part of the solution is the function
refreshSerchingToolbar
which I wrote. The function is not so simple, but it's simply in use:The last parameter is the same parameter which you used as
defaultSearch
property of the searching toolbar methodfilterToolbar
(the default value is 'bw', but I personally prefer to use 'cn' and set jqGrid parameterignoreCase: true
).If you fill the advanced searching dialog of the demo with the following field
and click the "Find" button, you will have the following grid:
(I marked the 'Total' column as non-searchable with respect of
search: false
to show only that all works correctly in the case also)One can see that all fields of the searching toolbar excepting "Amount" are filled with the values from the searching dialog. The field are not filled because we used "grater or equal" operation instead of "equal". The function
refreshSerchingToolbar
fills only the elements of the searching toolbar which can be produced by theJust as a reminder I should mention that in case of the usage of Filter Toolbar it is very important to define searchoptions.sopt options of the
colModel
. For all non-string column contains (dates, numbers, selects, int, currency) it is extremely important to have 'eq' as the first element of thesopt
array. See here and here for details.If you change the filter of the Advanced Dialog to the following
you will have as expected
At the end I include the code of the
refreshSerchingToolbar
function:UPDATED: The above code is no more needed in case of usage free jqGrid 4.13.1 or higher. It contains the new default option
loadFilterDefaults: true
of thefilterToolbar
, which refreshes the values of the filter toolbar and the filter operations (ifsearchOperators: true
option offilterToolbar
is ised) ifpostData.filters
andsearch: true
are set (the filter is applied). Free jqGrid refreshes the filter toolbar onjqGridAfterLoadComplete
(ifloadFilterDefaults: true
are set) or if the eventjqGridRefreshFilterValues
are explicitly triggered.I know it's an old post - but if you have multiple grids on the same page the above code can add the filter text to the wrong grid.
Changing this in the first loop in refreshSearchingToolbar, from
to
and this in the second loop from
to
should do the trick.
Kudos to Oleg