countrySelect
Purpose
创建一个HTML 并包含国家名称的选择框.
示例
// create a select for all countries
<g:countrySelect name="user.country" value="${country}"
noSelection="['':'-Choose your age-']"/>// create select from a list of ISO3166_3 country codes
<g:countrySelect name="user.country"
from="['gbr', 'usa', 'deu']"
value="${user?.country}"/>// create country select with internationalized labels
// expected properties in messages.properties:
// countryname.gbr=My United Kingdom
// countryname.usa=Home of the brave
// countryname.deu=Germany
<g:countrySelect name="user.country" from="['gbr', 'usa', 'deu'] valueMessagePrefix="countryname" />
描述
这个类提供一个选择框和所有 ISO3166_3 国家代码的option , 或者国家代码的list.
ISO 三个字母应格式化为小写字母.
属性
from
(必需) - 选择的list或范围.
value
(可选) -如果是from list中的元素,选择当前的值.
default
(可选) - 如果不设置默认为当前国家的 ISO 代码.
noSelection
(可选) -单例 map 处理 key 和 value 的对应,用于在选择框中没有这个选择项. 如果当前没有选择项,作为第一个list将被显示, 并且如果带这个选择提交,你提供的key将被提交. 通常这个是一个空.你也可以用"null"传递对象的ID.
valueMessagePrefix
(可选) - 依据ISO3166_3标准,默认的 "option" 元素将被显示为国家的英文名称.设置这个值将允许使用 I18n . 该 valueMessagePrefix 后缀将同一个 ('.') option的国家代码,解决信息问题 . 如果这个信息不能被解决,将显示该前缀加上国家代码.
来源
Show Source
def countrySelect = { attrs ->
if (!attrs['from']) {
attrs['from'] = COUNTRY_CODES_BY_NAME_ORDER
}
def valuePrefix = attrs.remove('valueMessagePrefix')
attrs['optionValue'] = { valuePrefix ? "${valuePrefix}.${it}" : ISO3166_3[it] }
if (!attrs['value']) {
attrs['value'] = attrs.remove('default')
}
out << select( attrs)
}