我们可以在g中有多个字段:select optionValue?

Hon*_*ney 13 grails

有没有办法在optionValue中显示多个字段名称?

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

EXPE:

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

Rev*_*nzo 40

如果您不想修改域类,可以为您的选项值传递一个闭包:

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


Dan*_*ann 10

您可以在域类中引入一个瞬态属性,您可以在g:select的optionValue中使用它:

class Book {
    String name
    String author

    static transients = [ 'nameAndAuthor' ]

    public String getNameAndAuthor() {
        return "$name, $author"
    }
}
Run Code Online (Sandbox Code Playgroud)

你的g:选择看起来然后像:

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


Sco*_*ren 5

或者将toString方法添加到Book类

public String toString() {
"${name} ${author}"
}
Run Code Online (Sandbox Code Playgroud)

然后省略optionValue

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

然后,当您在调试器中查看域时,它具有人类可识别的值.

希望这可以帮助.