Reputation: 1214
I wanted to use a g:select tag to produce a select box where the keys of the enum are displayed to the user, while the values of the enum are passed when the form is submitted, using the following enum:
public enum LetterRange {
ABCD("a-d"),
EFGH("e-h"),
IJKLM("i-m"),
final String value
AccessLevel(String value) { this.value = value }
String toString() { value }
String getKey() { name() }
}
Upvotes: 0
Views: 1915
Reputation: 1
You can use directly a closure to access your name property :
<g:select id="letterRange" name="letterRange" from="${LetterRange}"
noSelection="['':'Select Range...']" optionValue="${ {it.name()} }" required=""/>
Upvotes: 0
Reputation: 10665
I usually put my enum value on message.properties, this way I get the flexibility of adding any lang translation, also purpose you are looking.
So to do that I make my enum implement the MessageSourceResolvable class.
e.g ( source )
package com.mycompany
enum Season implements org.springframework.context.MessageSourceResolvable {
SPRING, SUMMER, AUTUMN, WINTER
Object[] getArguments() { [] as Object[] }
String[] getCodes() {
["${getClass().name}.${name()}"] as String[]
}
String getDefaultMessage() { name() }
}
message.properties
com.mycompany.Season.SPRING=Spring
com.mycompany.Season.SUMMER=Summer
com.mycompany.Season.AUTUMN=Autumn
com.mycompany.Season.WINTER=Winter
Gsp
<g:select required="${com.mycompany.Season.values}"/>
which will give you an Html like this:
<select>
<option value = "SPRING"> Spring </option>
<option value = "SUMMER"> Summer </option>
<option value = "AUTUMN"> Autumn </option>
<option value = "WINTER"> Winer </option>
</select>
Upvotes: 0
Reputation: 1214
I used the LetterRange enum:
public enum LetterRange {
ABCD("a-d"),
EFGH("e-h"),
IJKLM("i-m"),
final String value
AccessLevel(String value) { this.value = value }
String toString() { value }
String getKey() { name() }
}
...like so in the 'g:select' tag:
<g:select id="letterRange" name="letterRange" from="${LetterRange}"
noSelection="['':'Select Range...']" optionValue="key" required=""/>
...which produced the following HTML on the page:
<select id="batchRange" name="batchRange">
<option value="a-d">ABCD</option>
<option value="e-h">EFGH</option>
<option value="i-m">IJKLM</option>
</select>
Upvotes: 1