开发者

Can we have more than one field in g:select optionValue?

Is there any way to show more than one field name in optionValue?

<g:select name="id" from="${Books.list()}" optionKey="id"
          value="" optionValue="name"
          noSelection="${['null':'Select Publisher...']}"/>

Expe:

<g:select name="id" from="${Books.list()}" optionKey="id"
          value="" optionValue="name and author"
          noSelection="${[开发者_StackOverflow社区'null':'Select Publisher...']}"/>


You can pass a closure for your option value if you don't want to modify your domain class:

<g:select name="id" from="${Books.list()}" optionKey="id"
          value="" optionValue="${{it.name +' '+it.author}}"
          noSelection="${['null':'Select Publisher...']}"/>


You could introduce a transient property in your domain class, which you can use in the optionValue of the g:select:

class Book {
    String name
    String author

    static transients = [ 'nameAndAuthor' ]

    public String getNameAndAuthor() {
        return "$name, $author"
    }
}

Your g:select looks then like:

<g:select name="id" from="${Books.list()}" optionKey="id" value="" 
    optionValue="nameAndAuthor" 
    noSelection="${['null':'Select Publisher...']}" />


Or add a toString Method to your Book class

public String toString() {
"${name} ${author}"
}

then just omit the optionValue

<g:select name="id" from="${Books.list()}" optionKey="id"
          value="" noSelection="${['null':'Select Publisher...']}"/>

Atleast then when you look at the domain in your debugger it had a human recognizable value.

Hope this helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜