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

10,684
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()}" />
Share:
10,684
Alexx
Author by

Alexx

Seasoned software specialist with uncontainable creativity and entrepreneurial spirit. A driven perfectionist who does not settle for "good enough". Experience includes designing innovative solutions for eCommerce applications, including internet banking, consumer credit, and retail, spanning the entire lifecycle: systems analysis, software design, development, quality assurance, and project management.

Updated on June 05, 2022

Comments

  • Alexx
    Alexx almost 2 years

    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!