Accessing a list of params in controller - grails

Im very new to grails (1.3.7) so please be patient :-)
I have a gsp where I have various checkboxes. A user can click on them and then send his answer to a controller. The controller is receiving this request correctly.
My problem is, that, for working with what the user chose, I have to check every parameter - to see if this checkbox was really checked. Thats really cumbersome and doesnt work very well, because the page displaying the checkboxes is dynamic - so the checkboxes which can be clicked are dynamic too. In my controller I dont know for which params I have to check then.
Is there any possibility to receive a list of all checkboxes (or better: all checked checkboxes) in my controller? I researched but didnt find an answer!
Thanks for answering! :-)
[EDIT]
Thank you,
params.name.each{i->
System.out.println(i);
}
is very simple and works :-) It just gives back the checked ones

It must be passed as an extra request parameter (it's a limitation of http). You can add following field into your form, for example:
<input type="hidden" name="checkboxes" value="${myCheckboxesNames.join(',')}"/>
or making same using JavaScript, as it names are dynamic on client side.
BTW, you can also check all request parameters, by
params.each { name, value ->
// so something
}
so if you are using some special prefix/suffix for this checkbox names, it would be:
params.entrySet().findAll {
it.key.startsWith(prefix)
}.each {
println "Checkbox $it.key = $it.value"
}

Related

How do I get Grails g:select with multiple-selection with all selections when returning from the controller

I have a page that is a report from a database and I'm working on modifying how the filtering works. The intention is to allow the user to select possible values form a list that will be used to filter the resulting report. There are too many values to do this with checkboxes. I'm defining a multiple selection list box with this:
<g:select name="country" from="${countryDataList.KOUNTRY}" value="${params.country}" multiple="true" />
countryDataList is a List<> of objects with a name and a value which I create in the controller. I'm able to get the selected counties and process them without an issue.
But when the page returns from the controller with the filtered report, only the first selection in the list is selected. It doesn't re-select all of the items that the user selected. I am passing the params.country object back from the controller as
country:params.country
I saw some posts about this not working, but they are all from several years ago. Am I missing a vital step?
Ahh sorry, I was reading it on the phone initially and missed the point.
So what you want is a way of sending a multiple select box to a confirmation page. If I understand correctly?
Anyways how many objects in the select are we talking massive or a dozen couple of dozen or so ?
What I did was use check boxes and did a confirmation which shows the selection ticked in check boxes.. So this is the confirmation page that loads in https://github.com/vahidhedayati/mailinglist/blob/master/grails-app/views/mailingListEmail/confirmcontact.gsp
this page which is where multiple attachments selected from the schedule re-appear...
https://github.com/vahidhedayati/mailinglist/blob/master/grails-app/views/mailingListAttachments/_mailerAttachmentsDisplay.gsp.
Please note advice below is all conceptual stuff and there may be easier ways than this
Other than that You could create a taglib call on the confirmation page https://github.com/vahidhedayati/ajaxdependancyselection/blob/master/grails-app/taglib/ajaxdependancyselection/AutoCompleteTagLib.groovy#L55 which takes in your arrayList you could probably convert it to JSON pass it into the javascript that you load in within the taglib (on mine further down it loads this page in)
https://github.com/vahidhedayati/ajaxdependancyselection/blob/master/grails-app/views/autoComplete/_selectJs1.gsp#L23
and look to reselect them using javascript... as I say I haven't tested the last bit, the first bit i.e. checkbox works it is/has been in use.
Years later from you I just had the same problem. What I figured out is: it happens when params.country is an array instead of a Collection (i.e. an ArrayList).
A workaround for this if you want to stick to the array type is at the value attribute of the tag doing this: params.country?.findAll().

How to get the values true or false depending on a list of checkbox checked or not in Grails

I have check boxes in a list
HTML Code
<g:checkBox id="isBookable_${index}" class=" isBookable" style = "width :auto" name='isBookable' value='${careProviderScheduleExceptionInstance?.isBookable}' />
Problem
When I get the list of this checkbox i.e isBookable in params, the values for only the checkboxes that are checked comes in the list and the value comes as on and not true.
I want the values should come as true or false based on if the checkbox is checked or not.
Thanks in advance.
Grails g:checkbox does some shenanigans for the binding magic to work correctly when you do something like:
def schedule = new Schedule(params)
So that schedule.bookable would get populated correctly. I've never been a big fan of how it works but it is what it is and at the end of the day, it does work for typical use cases.
You have to remember that per the HTML spec, you're not going to get unchecked checkboxes passed in on a form submit. So looking for false is a moot point. What you would have to do is take the ones that do get checked (and submitted) and compare that against a list of possible bookable 'schedules'.
A work around would be to use Javascript to submit your form and pass in all the checkbox's with whatever value you want mapped to the correct instance.

Creating ajax-enabled subform with "Edit" button

I am looking for the best way to create ajax enabled subforms from items in a list with MVC 3. A static list of values should be generated, but with an "edit" link/button next to every item, to toggle inline edits.
I did follow the tutorial at this link
http://blog.janjonas.net/2011-07-24/asp_net-mvc_3-ajax-form-jquery-validate-supporting-unobtrusive-client-side-validation-and-server-side-validation [1]
However it is based on the form edit fields always being visible
I'd like to show a static list with field values, but let the user activate an edit field by clicking "edit" (e.g. button)
I did modify the example at [1] by creating a default partial view with a form with submit button only. When posting the data by ajax the edit form will show. It looks like it is working, (I only need to hide validation errors on the first POST - which does not send real data).
Update:
An even better solution would probably be to leave out all forms in the static view, just have a single css class button/link next to each item, and let jquery fetch the relevant view for the clicked item. I am not sure how to do that with MVC 3+jQuery though.
Another update:
I discovered Ajax.Actionlink, which did exactly what I wanted!
I found out how to do it, and it turned out to be real simple!
I created two partial views.
One for rendering each static item. I used used Ajax.ActionLink with InsertionMode "replace", and set the parent of the item as the target
The second for rendering the form. Here I used Ajax.Beginform with similar options.
On successfully saved data, I returned the static view, on failure, I returned the partial view with the ajax form again.
I'm happy I found a MVC-centric way to do it (although it is fun creating custom stuff with jQuery)
It sounds like you need an inline editing plugin for jQuery. I would try jEditable. I have not used it myself but appears to have extensive docs.
this entry might help: code + video + explanation ;)
http://ricardocovo.wordpress.com/2011/04/03/asp-mvc3-editing-records-with-jqueryui-dialogs-and-ajaxforms/
-covo

Forgot how to do non-ajax dropdown in MVC

I know this sounds silly but i forgot how to code non-ajax.
Specifically:
I am in MVC
I have a dropdown list with languages.
When a language is chosen i want to reload the whole page with the new language.
This all works but I have to manually refresh the page.
I mean I could call window.location.refresh after i return from the action but i feel like i should be able to do a full refresh. Am i suppose to call submit on a form?
I really feel like i am missing osme extremely easy right in front of my face thing.
I have been doing so many partial ajax updates in my life,i lost my plain old post and reload.
Yea you have to do a form submit, but whats wrong with window.location.refresh?
Normally selecting language for a site is independent of other form submissions, and you can get away with a GET instead of POST. So, in the onchange attribute of your select, you can put:
var lang = ...//get selected language value here
document.location = 'http://mysite.com/' + lang //or whatever your URL scheme is

MVC DropDownList SelectedItem Value in ActionLink

I'm a bit confused and sorry if this question is repeated elsewhere, I did check and it didnt seem to be here yet.
Is there a way, (without use of JavaScript) to get the currently selected item of a DropDownList and say send it off to an ActionLink?
<%= Html.DropDownList("Elements") %>
<%=Html.ActionLink("Add Authorization Element", "AddElement", new {elementGuid = ??? }) %>
The bit I am looking for is something to replace:
???
Thanks,
Ric
Not without JavaScript, no. Of course, it's trivial with JavaScript.
If you want to do both, add JavaScript to the drop down, then put a submit button inside a noscript tag. Users without JavaScript will have to click the button. Users with JavaScript won't see it.

Resources