select

用途

创建一个HTML的 selects 标签.

示例

// create a select from a range
<g:select name="user.age" from="${18..65}" value="${age}"
          noSelection="['':'-Choose your age-']"/>

// use a no selection with a nullable Object property (use 'null' as key) <g:select id="type" name='type.id' value="${person?.type?.id}" noSelection="${['null':'Select One...']}" from='${PersonType.list()}' optionKey="id" optionValue="name"></g:select>

// create select from a list of companies // note the 'optionKey' is set to the id of each company element <g:select name="user.company.id" from="${Company.list()}" value="${user?.company.id}" optionKey="id" />

// create select with internationalized labels (this is useful for small static lists and the inList constraint) // expected properties in messages.properties: // book.category.M=Mystery // book.category.T=Thriller // book.category.F=Fantasy <g:select name="book.category" from="${['M', 'T', 'F']}" valueMessagePrefix="book.category" />

仅仅作为一个方法在GSP中调用:

${select(from:aList,value:aValue)}

描述

属性

值得一提的是 <g:select> 的 optionKeyoptionValue 属性,在<select>标签的结果中,允许控制向用户显示的内容,则值是form表单提交的.默认的行为是调用from属性里每个元素的toString() , 但如如果在Book domain 类是有个list,这个行为的用处不是很大.

下面一个例子 <g:select>optionKey 属性解决每个Book的id属性,作为每个 <option> 标签value属性的值.用<g:select>的optionValue 属性显示每个Booktitle 属性给用户:

<g:select optionKey="id" optionValue="title" name="book.title" from="${bookList}" />

如果你需要更多的控制,可以在optionValue属性中用闭包做个转换,再把每个<option> 元素给用户. 下面一个例子, 把Book 的title属性值转换为大写:

<g:select optionKey="id" optionValue="${{it.title?.toUpperCase()}}" name="book.title" from="${bookList}" />

来源

Show Source