【问题标题】:How to implement dummy values in Grails g:selects?如何在 Grails g:selects 中实现虚拟值?
【发布时间】:2015-01-04 06:41:09
【问题描述】:

在纯 HTML 中,我可能有一个看起来像这样的 <select>

<select id="colors">
    <option id="0" selected="selected" value="choose">Choose a color</option>
    <option id="1" value="red">Red</option>
    <option id="2" value="blue">Blue</option>
    <option id="3" value="green">Green</option>
</select>

因此第一个&lt;option&gt; 只是一个“虚拟”值;也就是说,它只是向用户解释他们必须选择一种颜色的填充文本。我什至可能会在其上添加一些样式,使其显示为灰色/禁用,以便用户知道这不是一个可行的选择。

但在 Grails 中,我们将模型注入 select:

// Inside ColorsController
def index() {
    // Returns a list of 3 colors for: R, G, B
    List<String> colors = getRedGreenAdnBlue()

    render(view: "colors", model: colors)
}

// Inside colors.gsp
<g:select name="colors" from="${colors}" />

但这只会产生:

<select id="colors">
    <option id="1" value="red">Red</option>
    <option id="2" value="blue">Blue</option>
    <option id="3" value="green">Green</option>
</select>

看看我现在如何失去了“选择一种颜色”的虚拟值?我知道我可以在页面加载时执行一些 jQuery 以将此值插入选项列表的顶部,但我想知道这是否是 Grails 已经具备处理能力的使用模式。所以我问:有解决这个问题的 Grails 方法吗?

【问题讨论】:

  • 使用 g:select 标签的 noSelection 属性怎么样?

标签: html grails select gsp


【解决方案1】:

使用 noSelection:

<g:select name="colors" from="${colors}" noSelection="['':'Choose a color']"/>

这里是文档:http://grails.org/doc/latest/ref/Tags/select.html

【讨论】:

    猜你喜欢
    • 2012-03-12
    • 2015-04-18
    • 2015-05-02
    • 1970-01-01
    • 1970-01-01
    • 2016-06-15
    • 1970-01-01
    • 1970-01-01
    • 2013-02-27
    相关资源
    最近更新 更多