SetInitialFilter() not working with an empty query string

Jun 25, 2013 at 5:06 PM
Edited Jun 25, 2013 at 6:02 PM
SetInitialFilter() does not work with an empty query string because _settings.IsInitState only returns true if grid-init != null. As a workaround I changed IsInitState to return true if there is no grid-filter in the query string and set the filter for the column if the _settings.IsInitState && InitialFilterSettings != default(ColumnFilterValue). If a user clears all filters it will leave grid-filter= with no value.
Coordinator
Jun 25, 2013 at 5:25 PM
Hi,
Yes SetInitialFilter uses to predefine initial filter for the user. User can clear all filters and view all grid data - this is purpose of initial filtering. If you want that user can't to view all grid data - you need to filter your initial collection and pass it to the grid:

example:
var items = _repository.GetAll().Where(u=>u.UserId == currentUserId);
return View(items);
Jun 25, 2013 at 6:01 PM
But when I use SetInitialFilter() it doesn't filter the column unless grid-init is in the query string. For instance in the demo if I add
.SetInitialFilter(GridFilterType.Equals, "true")
to the IsVip column it only works if I add ?grid-init to the url.
Coordinator
Jun 26, 2013 at 2:34 AM
Yes
Because empty url means that no filters would be applied. Grid redirect to empty url when user clear default filter.

If you want to navigate user to page with initial filter - you need to redirect to grid page with ?grid-init query parameter.
Jun 26, 2013 at 6:03 PM
Ok, I do see that in the documentation now. Personally, I prefer for an empty url to use the default filter and ?grid-filter= to clear the default filter which could be done by removing them individually via the filter widgets or adding a clear all link to the table. Overall great work on the control. I'm replacing all my table with this.
Aug 24, 2013 at 8:44 PM
Edited Aug 24, 2013 at 9:07 PM
I tend to agree here, that the most clean solution would be to let an empty URL apply the default filter. And have a special parameter like ?grid-clear or just ?grid-filter= to clear the initial filtering.

[Edit1] Another good reason for doing that is the Html.ActionLink helper method does not support route parameters (like query string parameters) with a dash in the name. So it clutters the view to link to a grid with initial filters.

[Edit2] Ah, I found a way, with RouteValueDictionary it is possible to have query string params with dashes in them.

However now when I have grid-init=1 it sets initial filters on EVERY field, with weird null values for those not having any inital filter. Strange :S
Coordinator
Aug 26, 2013 at 4:39 AM
Silwing, Hi

Using empty url as default more complex, because now when you click "clear filter" column clears only self filter settings (if multiple columns filtered). In this case I can't imagine when the column should redirect to ?grid-clear url. Any ideas?

It seems a problem with determining of default filters in current version. I've fixed it in https://gridmvc.codeplex.com/SourceControl/changeset/26056
Aug 26, 2013 at 12:47 PM
When you click on a clear filter link it's still going to pass the other default filters that may be applied. If there are no more applied, then pass ?grid-filter= and use that to override the default filters. You could easily have a clear all filters link somewhere that just passes that as the query string. In my case, I am usually just filtering out inactive records unless they explicitly want to see those, then they can change the filter.
Coordinator
Aug 27, 2013 at 2:36 PM
Ok, I'll look what can I do.
New issue: https://gridmvc.codeplex.com/workitem/1189
Sep 26, 2013 at 5:25 PM
Edited Sep 26, 2013 at 6:14 PM
it would be really helpful if you could drop the dash, then we could redirect to a view with default filters with something like:
return RedirectToAction ("myActionName", new { gridinit = "1" });
Of course the solution of Silwing works already, but has a bit more complicated syntax:
return RedirectToAction("myActionName", new RouteValueDictionary {{"grid-init", "1"}});
Coordinator
Sep 27, 2013 at 4:53 AM
RobertWagenaar, you're right. I'll fix it at next release