grails g:select标签使用List>作为from标签数据源

GSP页面中 g:select 用于创建一个HTML的 selects 标签.
下面例子里面用到的g:select 属性有
from (必需) - select 的范围
value (可选) - from范围内当前的选择值.
optionKey (可选) -用于指定生成的HTML selects标签中<option>元素的 value 属性
optionValue (可选) - 用于指定生成的HTML selects标签中<option>元素  显示内容
value (optional) - The current selected value that evaluates equals() to true for one of the elements in the from list.
noSelection (optional) - A single-entry Map detailing the key and value to use for the "no selection made" choice in the select box. If there is no current selection this will be shown as it is first in the list, and if submitted with this selected, the key that you provide will be submitted. Typically this will be blank - but you can also use 'null' in the case that you're passing the ID of an object
valueMessagePrefix (Optional) - By default the value of the option element will be the result of a toString() call on each element in the from attribute list. Setting this allows the value to be resolved from the i18n messages. The valueMessagePrefix will be suffixed with a dot ('.') and then the value attribute of the option to resolve the message. If the message could not be resolved, the value is presented.
multiple (optional) - Set to true to generate a multi-select listbox rather than a dropdown list.

<g:select optionKey="queryBy" optionValue="queryShow" from="${[[queryBy:'newsTitle',queryShow:'新闻标题'],[queryBy:'newsAuthor',queryShow:'新闻作者'],[queryBy:'newsContent',queryShow:'新闻内容']]}" name="queryBy" value="${params.queryBy}"></g:select> 

其中的关键是从如何拼装 [[queryBy:'newsTitle',queryShow:'新闻标题'],[queryBy:'newsAuthor',queryShow:'新闻作者'],[queryBy:'newsContent',queryShow:'新闻内容']]这种类型的对象,按照groovy的语法,应该是List<Map<String,String>>这种类型的对象。
     
这个例子是我在做查询时使用select元素让用户按何查询,关键是自定义from属性中的映射然后用optionKey、optionValue指定<option>元素value 和显示内容。
生成的HTML 代码如下

<select name="queryBy" id="queryBy" >  
<option value="newsTitle" selected="selected" >新闻标题</option>  
<option value="newsAuthor" >新闻作者</option>  
<option value="newsContent" >新闻内容</option>  
</select>  

selected="selected" 是根据g:select的value自行判断并生成

你可能感兴趣的:(grails)