Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using value of enum in g:select when enum is attribute of selection object

Example:

batchTag is an enumerated type attribute of a batchRange, with values like so:

JAN1 "January Biweekly 1",
JAN2 "January Biweekly 2",

etc.

I want to display the VALUE of the batchTag in the select, IOW, the select should contain

"January Biweekly 1"
"January Biweekly 2" ...

not

JAN1
JAN2
FEB1
FEB2
FEB3 ...

I have tried several things in the g:select to do this, but without any success. I thought perhaps "it" would be available as part of the g:select (as it is clearly an iteration) and tried to reference it.batchTag.name for the optionValue, but that did not work. Any suggestions?

Thank you!

like image 943
Alexx Avatar asked Jun 20 '12 18:06

Alexx


People also ask

Can enums have attributes?

An enum can, just like a class , have attributes and methods. The only difference is that enum constants are public , static and final (unchangeable - cannot be overridden).

Can enum value be object?

An enum is a data type that can be created by a Java programmer to represent a small collection of possible values. Technically, an enum is a class and its possible values are objects.

How do I query enum in SQL?

ENUM values are sorted based on their index numbers, which depend on the order in which the enumeration members were listed in the column specification. For example, 'b' sorts before 'a' for ENUM('b', 'a') . The empty string sorts before nonempty strings, and NULL values sort before all other enumeration values.

Can you index enums?

Yes you can programmatically index an enum, text, or menu ring.


1 Answers

enum BatchRange {
    JAN1 "January Biweekly 1",
    JAN2 "January Biweekly 2",

    final String value

    BatchRange(String value) { this.value = value }

    String toString() { value } 
    String getKey() { name() }
}

Note the getKey() method. And then your g:select

<g:select name="batch" from="${BatchRange.values()}" optionKey="key" />

or

<g:select name="batch" from="${BatchRange.values()}" keys="${BatchRange.values()*.name()}" />
like image 159
Gregg Avatar answered Sep 24 '22 16:09

Gregg