I'm working on a Grails application. There is a domain class "participant" - within the view there is a possibility to select some filters. Those filters get applied through the corresponding controller. Once the filter has been applied to the selection, the result gets posted.
Now the problem is, that I don't know how to revert those filters or clear them. Obviously, a simple HTML reset button within the view won't work as the filtered result are POSTed.
Could anyone tell me, how to clear the filters used? I don't want a hardcoded href which redirects the user to the normal "list" action. Any idea(s)?
There are many ways to do what (I think) you want to do. One idea is to create another action in the same controller as the filter action that returns the list you want. I could try to give you an example but from the code you posted it isn't clear which view you're coming from, which view should be shown after clearing the filter or where the hardcoded id (1) id coming from. Also, is it possible that the code you posted had been modified in an attempt to solve this? The getParticipants method doesn't appear to be called at all.
If you wanted to answer some of these questions: starting view, target view (after clearing filters), controller involved and where the id is coming from, I could try to give you a better answer.
Thanks for the extra info. Here's one possible solution. I didn't try this so the syntax may not be exact but it'll get you started:
It looks like what you would get without the filters is all the participants for a Conference. If that's correct then you can add an action like this to ParcipantController.groovy
def clearFilter = {
def conference = Conference.get(params.id)
def participants = conference.participants
render(view:'list', model:[participants: participants,
participantsTotal: participants.size(),
startDate:conference.start,
endDate:conference.end,
canWrite: accessRightsService.canWrite(request.beholder, conference)])
}
Then you can add a tag to your page instead of the hard-coded anchor tag like this:
Clear Filter
I hope that helps,
Dave
Related
So, I was thinking of doing something like this:
zones#show
/map/map_id/zones/zone_id
And this:
zones#index
/map/map_id/zones
But I would like the user to be able to display more than one zone at the same time (but not all of them), so I was thinking of something Reddit-like:
zones#show
/map/map_id/zones/zone_id_1+zone_id_2+zone_id_3
The ID would reach the controller as one parameter and then split between the "+" to make an array, nothing fancy in the routes.
Would this still be RESTful? Is this the best approach?
The show RESTful action infers displaying one of a type of resource. The index action infers displaying a collection. As far as I know nothing in the latter implies that you must be showing every single instance (i.e. that it cannot be filtered...even if the filtering process only leaves only a small subset). So I would suggest using the index action and just showing the desired instances.
You might want to also check out this article:
http://www.informit.com/articles/article.aspx?p=1671632&seqNum=11
I am in the process of writing a web app that includes a reporting form. This form contains a number of radio buttons that allow the user to specify the return data.
There are about 6 different return data 'formats', and each of those has two variations - html data or JSON data for rendering to a chart.
I have begun coding it up and already my form post action method feels wrong.
I basically have a check on the requested data format and return it as needed. Each return type requires its own partial view / json object so there is little room for reusing code.
It feels like each one should have its own action method. Having the form post to different locations based on a radio button choice also feels wrong though.
Switching on report type and then redirecting to the appropriate action in the controller also feels like its not quite right.
Am I approaching this in the wrong way? As it currently stands my controller action contains a lot of code and a lot of logic...
Hope my query makes sense.
Thanks
I don't think there is anything wrong with your approach. To maximize reuse you could:
include reusable templates inside your views
make sure the business/data layer code is the same everywhere (where possible)
I suppose the views you need to return actually are different for each combination of options so whatever approach you take, you are stuck with that.
I wouldn't opt for the client-side approach. You then have code on both the server and the client that has to be updated whenever you change anything. I would keep the code that receives a set of options and determines what to do with them in one place.
I know what you mean about it feeling like each format should be a separate action, but maybe a hybrid approach would make it feel better.
If you set the value of each radiobutton to the name of the action it relates to, you then, in your main POST action, have a parameter that you can use to call the appropriate action in one line of code. You don't have to fudge anything in Javascript, it's easily extensible, and you get your separate actions.
If I understand your problem right you have a lot of switch code in action.
I think you can use Factory pattern. You can create factory that will accept switch parameter as parameter and will return ActionResult instance.
Explanation: I have a grid with a checkbox column where users can check records they want to view details of. Each checkbox has a value of the ID of that object. I want to then pass these values to a controller action so that I can render a paged view, as it were.
Now, what I have right now is technically working, but it's ugly and something tells me there's a better way. Right now I'm creating a JSON string of the array values and passing that, so the route ends up looking like "/Products/Details/["4","5","6"]"
Now, as I said, this works and I can parse out the values in the controller, but something tells me I shouldn't have to. Am I missing something simple, or does someone know of a decent way of doing this? The number of values can be arbitrary based on what the user has selected, so I can't create a custom route (I don't think, anyway).
Thanks, all.
If you haven't read Phil Haacked - Model Binding To A List I would highly recommend starting there, it should help alot.
As the subject says, is this a bad idea? If so, why?
Currently, if you are rendering some input fields from inside a #Html.Acion, the validation errors don't get displayed to the user as the ModelState gets cleared when the #Html.Action gets involved (in its context.)
So whats the best pattern around it?
You can access the parent contexts via ControllerContext.ParentActionViewContext or ViewContext.ParentActionViewContext (details here) but I think there are better solutions.
A child action is a good choice when you do not want to pollute all of your view models with data which are available independent from the current controller action and view (for example a user welcome label, a navigation bar, etc.).
For other reuse scenarios like common input fields a partial view is a better approach.
However if you give more details about your current scenario I try to suggest a more specific solution.
I have an action that populates the result from the DB. Right now, I see a way of doing it and that is to make the Action ServletRequestAware, set the populated list as a request attribute and show it in jsp.
Since a lot of improvements have been introduced into struts2, is there any other way of doing that? The first thing that comes to my mind is to use displayTag and change the return type of Action to a List, but that does not seem to work.
Thanks for any replies in advance.
You question is unclear, you should read some book about Struts2 to get the general idea.
No need to make the Action ServletRequestAware. The mapping from http parameters to actions fields is automatically done via the Param interceptor (already set in the default configuration). And one of the points of Struts2 is decoupling the action from the http protocol, you should not (typically) do anything related to http in your action.
Tipically, in your action execute() method (or whatever) you'll get the data to display from the DB and set it as one property of your action, so that is accesable from some getter.
Then, in your view page (JSP or whatever) you'll display it. You can use displayTag, but first you'll prefer to display it "manually", to understand what's involved. See for example here http://www.roseindia.net/struts/struts2/struts2controltags/iterator-tag.shtml
For manually displaying a table, also see this example http://www.vaannila.com/struts-2/struts-2-example/struts-2-crud-example-1.html , search for the <table class="userTable> tag.