Grails< g:if> in< g:select> [英] Grails <g:if> in <g:select>

查看:110
本文介绍了Grails< g:if> in< g:select>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 .gsp 文件中有< g:select> 。但不同于任何普通的< g:select> ,这个人会拥有属性 disabled =

以下代码:

 < / p> g:select name =test
from =$ {[foo1,foo2]}

它返回一个错误: Grails标签[g:select]未关闭



但是,当我将其更改为此项时:

 < g:select name =test
from =$ {[mu1,mu2,mu3]}
$ {if(true){println disabled = \\}} />

它返回了这个错误:必须引用属性值。 $ b

这两个错误消息都在异常下, org.codehaus.groovy.grails.web.taglib.exceptions.GrailsTagException



问题是我们该如何做这项工作?有没有可能的答案,而不使用自定义的TagLib?

解决方案

GSP表单字段标签treat disabled 作为一个布尔属性,所以你可以说

 < g:select .... disabled = $ {true}/> 

通常情况下,您应该可以在通常的Groovy-truth规则下使用任何表达式,但我相信它会使字符串true和false的特殊情况(后者在Groovy-truth规则下通常被认为是 true ,作为非空字符串)。如果有疑问,你可以总是说:
$ b $ pre $ disabled =$ {(someExpression)as boolean}


I have this <g:select> in a .gsp file. But unlike any ordinary <g:select>'s this one would have the attribute disabled="" if a certain condition is met.

Following the code:

<g:select name="test" 
          from="${["foo1","foo2"]}" 
          <g:if test="${true}">disabled=""</g:if> />

It returned an error: Grails tag [g:select] was not closed

But when I change it into this:

<g:select name="test" 
          from="${["mu1","mu2","mu3"]}" 
          ${ if(true) { println "disabled=\"\"" } }/>

It returned this error: Attribute value must be quoted.

Both of the error message are under the exception, org.codehaus.groovy.grails.web.taglib.exceptions.GrailsTagException

The question is how could we make this work? Is there a possible answer without using a custom TagLib?

解决方案

The GSP form field tags treat disabled as a boolean property, so you can say

<g:select .... disabled="${true}" />

Generally you should be able to use any expression under the usual Groovy-truth rules but I believe it makes a special case for the strings "true" and "false" (the latter would normally be considered true under Groovy-truth rules as a non-empty string). If in doubt you can always say

disabled="${(someExpression) as boolean}"

这篇关于Grails&lt; g:if&gt; in&lt; g:select&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆