Grails <g:if> in <g:select>

有些话、适合烂在心里 提交于 2019-12-11 07:43:12

问题


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?


回答1:


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}"



回答2:


No need to use the println, try this

<g:select .... ${(conditional)?"disabled":""} ... />



回答3:


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

is fine but when you submit and it is a required field the value will not be submitted so use this jQuery code to enable the field when pressing the submit button

    $(function() {

        $('form').on('submit', function() {
            $(this).find(':disabled').removeAttr('disabled');
        });

    });


来源:https://stackoverflow.com/questions/12507918/grails-gif-in-gselect

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!