GSP select from multiple lists - grails

Given two similar domains Foo and Bar, is it possible to create a g:select tag that can choose from both of them? E.g.,
<g:select from="${[Foo.list(), Bar.list()]}"/>
That, unfortunately, does not work. It creates a single option that is the text of all items from both domains :|
So, does anybody know if this can be done?
Edit
Nope, it doesn't make any difference to pass this list in from the controller.

<g:select from="${Foo.list() + Bar.list()}"/>
would also do just fine

Whoops, just needed to flatten the list!
<g:select from="${[Foo.list(), Bar.list()].flatten()}"/>
Otherwise, it was a list of lists.

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 change the number pattern in gsp?

I have one domain class which has one numeric field. Default scaffolding generated it. When I enter a number with more than 3 digits, ex:- 1234, then in show.gsp and list.gsp it shows as 1,234. My requirement is to show the number without comma. I can't use g:formatNumber because I have more than 50 domains and not able to use this tag for every gsp page. Is there anything I can do to change the number pattern globally?
thanks
maybe solution suggested here can be useful
Get the field directly like:
<td valign="top" class="value">${formulationTypeInstance?.id}</td>
Of course you may lose some of the tag's features with it, like highlighting field when a validation error occurs. But you're in show.gsp and it shouldn't have text fields or something to submit, so you lose nothing! Simple is the way to go here. =)

How to show or hide fields on a GSP based on a selection in a combo-box

I'm still new to grails, so I'm really sorry for asking something like this.
I have a domain that could be divided in 3 types, so I'd like to create a combo-box and depending on which selection the user made, it will show some fields and hide others. How can I achieve this?
Thanks again and sorry for this dumb question.
This is actually something that is commonly done with JavaScript. You could attach an 'on change' event handler to your combo-box that would show/hide the appropriate elements as needed.
In case you're not familiar with how to do this with Javascript, I'd recommend you take a look at jQuery: http://api.jquery.com/change/

Accessing a list of params in controller

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"
}

naming form files using square brackets

I've noticed in some php applications, that form fields are labelled with [] in them.
Say a shopping cart page, that lists all the items where you can edit the quantity.
is the [] type naming used to get the correct row?
Just trying to figure out how I should name each textbox?
should it be like:
name="quantity-<%= items.RowId %>"
THen when I loop the form fields, I would get the index number?
This is done in PHP when you need to pass <select multiple="multiple"> to the server, otherwise PHP will catch only the first selected value.
Textboxes don't normally require such naming.
This might answer a few of your questions. Here, Phil Haack uses square brackets in order to bind to a model that contains a list.

Resources