1

我已经对如何显示带有可选值的下拉列表进行了一些研究。目前,我在下拉列表中有一个域对象(人)列表。它们显示为“名字姓氏”。

我想更改名称在选择中的显示方式。查看 g:select API,我发现我可以使用 optionValue 和 optionKey。我试过了,但无处可去。所以这是我当前的代码:

<g:select id="allPeople" name="allPeople" from="${dropdown}" />

我希望输出为“Last, First”。我试过这个:

<g:select id="allPeople" optionValue="${it?.last},${it?.first}" from="${dropdown}" name="allPeople" value="" >
4

2 回答 2

4

一个不错的方法是在您的域上添加一个临时 getter,然后将其用于 optionValue。

class Person {
    String first
    String last

    static transients = ['lastFirst']

    String getLastFirst() {
        "$last, $first"
    }
}

然后

<g:select name="allPeople" optionValue="${it.lastFirst}" from="${dropdown}"/>
于 2012-08-30T14:57:50.727 回答
2

试试这样:

optionValue="${{it?.last+', '+it.first}}"

这意味着你传递了一个构建你需要的东西的闭包。

于 2012-08-30T14:52:19.970 回答