I've done some research on how to display a dropdown with an optional value. Currently, I have a list of domain objects (people) in a dropdown. They display with "First name last name".
I want to change the way the names are displayed in a selection. Looking at the g:select API, I see I can use optionValue and optionKey. I've tried, but got nowhere. So here is my current code:
<g:select id="allPeople" name="allPeople" from="${dropdown}" />
I want the output to be "Last, First". I've tried this:
<g:select id="allPeople" optionValue="${it?.last},${it?.first}" from="${dropdown}" name="allPeople" value="" >
A nice way to do it is to add a transient getter on your domain, then use that for the optionValue.
class Person {
String first
String last
static transients = ['lastFirst']
String getLastFirst() {
"$last, $first"
}
}
then
<g:select name="allPeople" optionValue="${it.lastFirst}" from="${dropdown}"/>
Try like this:
optionValue="${{it?.last+', '+it.first}}"
This mean that you pass a closure that builds what you need.
Related
normally I use command objects to work with submitted values. But if multiple values of the same property are submitted via AJAX (using jQuery) I was not able to use command objects.
In my GUI the user can click on checkboxes to mark some objects. Let's assume the name of the checkboxes is provider, i.e.
<input type=checkbox name=provider value=1>
<input type=checkbox name=provider value=2>
and so on...
When the clicked values are submitted via AJAX, in the Grails controller these values are in an map:
params.'provider[]'
where the key is provider[] and the value is an array of Strings if multiple checkboxes are clicked, otherwise it's just a String.
The problem is, that I can not create a command object with a property named provider[]. What I tried was:
class MyCommand {
Long[] provider
// or
List<Long> provider
}
but that didn't work.
So, my question is, how can I use a command object in this case? I want Grails to do the mapping, I do not want to do the mapping myself.
I am using Grails 2.3.11.
Thanks in advance,
best regards,
Daniel
To use command object, change your check box name to provider[index]
<input type="checkbox" name="provider[0]" value=1>
<input type="checkbox" name="provider[1]" value=2>
and so on...
and change your command object --
import org.apache.commons.collections.FactoryUtils
import org.apache.commons.collections.ListUtils
class MyCommand {
List<Provider> provider = ListUtils.lazyList([], FactoryUtils.instantiateFactory(Provider))
}
On my view, instead of using a normal <g:textField> field in the form, I would like to make use of Grails' <g:countrySelect> widget.
My problem
<g:countrySelect name="country" value="${myObjInstance?.country}"/>
...produces...
<select name="country" id="country">
<option value="afg">Afghanistan</option>
<option value="alb">Albania</option>
...
</select>
This would mean that, when the form is submitted, it will assign the value as country code (e.g. "afg") instead of the country name ("Afghanistan"). I prefer using the full country name as value. If possible, how do I achieve this?
I don't believe the taglib as is gives you that ability. You would either need to extend it or use your own Country collection + <g:select />
Another option would be to use this enum of the ISO_3166 country codes to get the full length description. Eg:
CountryCode.getByCode(params.country).getName()
To answer my own question...
The values can stay as is (country codes) and then just use country codes. On your views, you can make use of the grails country() method to display the country name, e.g.
Let's say the country code is "afg" (the value saved in your table). On your view (GSP) you can use
<div>${country(code: fieldValue(bean: myObjInstance, field: "country"))}</div>
This will print out
<div>Afghanistan</div>
Instead of
<div>${fieldValue(bean: myObjInstance, field: "country")}</div>
...that was used before:
<div>afg</div>
I'm using the Grails framework. In my User controller, I have a boolean field named "active" which controls if users are allowed to login. The login action checks this value when the user is logging in.
My domain:
class User {
Boolean active
}
My view (edit.gsp):
<g:select id="active" name="active" from="${[1,0]}" value="${userInstance?.active}" />
The value saves correctly into the database, but I want the Account Status dropdown to say "Enabled" or "Disabled", instead of "1" or "0" as it does now.
It also should show the current value when the Edit page is loaded. Currently, it always shows the value of "1", even if the user has the value of "0" in the database.
This seems like it would be very easy, but I haven't been able to find any examples of anyone setting their dropdown values in the GSP, and nothing I've tried so far is working. Thanks!
I see two solutions, both in the documentation.
One is to us the keys parameter of the tag:
<g:select id="active" name="active" from="${['Enabled','Disabled']}" keys="${[1,0]}" value="${userInstance?.active}" />
This provides a different list of keys vs the list of values.
The other solution is to use the optionKey and/or optionValue parameters, but this is would would require the list to contain objects or something similar that could be used to look up the values:
In src/groovy/BooleanSelectOption.groovy:
class BooleanSelectOption {
String name
String value
private BooleanSelectOption(name, value) {
this.name = name
this.value = value
}
private static List _list;
public static List getList() {
if(!BooleanSelectOption._list) {
BooleanSelectOption._list = [new BooleanSelectOption('Enabled',1), new BooleanSelectOption('Disabled',2)]
}
BooleanSelectOption._list
}
public String toString() { name }
}
In your view:
<g:select id="active" name="active" from="${BooleanSelectOption.list}" optionKey="value" value="${userInstance?.active}" />
Now the tag is looking up the key based on a bean property of the items in the list. Also, an enum might work here, too.
Obviously the first technique is cleaner for short lists, but I wanted to show both options for more complex situations. I haven't tested the second example, either.
One more note: You will probably find that the keys 0 and 1 don't really work, because Disabled will not get selected (in my experience) if the value is false. I don't know if you can get away with using true and false, but you should test to make sure you are getting what you expect.
There's actually a third option, probably the most robust solution, also in the docs:
Use the valueMessagePrefix parameter to allow the displayed value to be looked up from the i18n messages.
In grails-app/i18n/messages.groovy:
boolean.select.0=Disabled
boolean.select.1=Enabled
In your view:
<g:select id="active" name="active" from="${[1,0]}" value="${userInstance?.active}" valueMessagePrefix="boolean.select" />
This has the additional benefit of allowing you to have different labels for different languages, if you ever need it.
You can use optionKey and optionValue to use an object property or map value for the keys and values. Try something like this:
<g:select name="active" optionKey="key" optionValue="value"
from="${[[key: 1, value: 'Enabled'],[key: 0, value: 'Disabled']]}"/>
Just as an alternative for cases like this when there appears there will be much more processing involved than the task warrants remember you can just fallback to plain old html. E.g.
<select name="active">
<option value="0" ${!active ? 'selected' : ''}>Disabled</option>
<option value="1" ${active ? 'selected' : ''}>Enabled</option>
</select>
I have a select tag in my GSP file as
<g:select name="clientId" id="clientId" size = "4" from="${com.springcommunity.fleet.partymodel.roles.ClientRole.list()}" class = "filter_combo" optionKey="id" />
i want client with id 2 is selected initially (in simple html it is achived by using selected="selected")
how can i do it?
You need to specify the value attribute in this tag. http://grails.org/doc/2.0.x/ref/Tags/select.html
So in your example,
<g:select ... value="${com.springcommunity.fleet.partymodel.roles.ClientRole.get(2)}" />
One thing to be aware of here is that the value that you're selecting must be an object equal to the item in the list, and not just an id - this is where a lot of people get tripped up. So you can't just say value='2', you need to specify the object in the list that you have in your from attribute.
From the docs -
value (optional) - The current selected value that evaluates equals()
to true for one of the elements in the from list.
In action class i am reading value from the database.
Let's say "abc".Now the "abc" value should be populated to jsp page.i.e "abc" value should set to s:hidden field in the jsp page.
Since it is single value , i don't want to use List in the action class.
Is there any other way to do that ?
Why would you want to use a list? Just provide the appropriate getter like:
public Object getAbc(){
return abc;
}
and in your page access it with simple OGNL expression like:
<s:hidden name="filedYouWantToSetThisValueTo" value="%{abc}"/>
Hope I got it right.