[英]How do I check in GSP Grails more than one time in list
I would like to do a check in a GSP page. 我想在GSP页面中进行检查。 I have a list of products.
我有一个产品清单。 the list contains about 21 products.
该列表包含约21个产品。 I use g:each to loop the products in GSP and if/else for the check.
我使用g:each在GSP和if / else中循环检查产品。 see my code below.
请参阅下面的代码。 but if the product.id not eaqual to 3. The page displays/prints the checkBox "dontCallMe" 21 times and I expect to print it one time.
但是如果product.id不等于3,则该页面显示/打印“ dontCallMe”复选框21次,我希望将其打印一次。 why is that?
这是为什么? is there other way to do the check?
还有其他检查方法吗? thanks
谢谢
<g:each in="${products}" var = "product">
<g:if test="${product.id == '3'}">
<tr>
<td>
<g:checkBox name="callMe" checked="true"
value=""/> Call me
</td>
</tr>
</g:if>
<g:else>
<tr>
<td>
<g:checkBox name="callMe" checked="false"
value=""/> Call me
</td>
</tr>
</g:else>
<g:if test="${product.id == '4'}">
<tr>
<td>
<g:checkBox name="callMeAgain" checked="true"
value=""/> Call me again
</td>
</tr>
</g:if>
<g:else>
<tr>
<td>
<g:checkBox name="callMeAgain" checked="false"
value=""/> Call me again
</td>
</tr>
</g:else>
<g:if test="${product.id == '5'}">
<tr>
<td>
<g:checkBox name="dontCallMeAgain" checked="true"
value=""/> Dont call me again
</td>
</tr>
</g:if>
<g:else>
<tr>
<td>
<g:checkBox name="dontCallMeAgain" checked="false"
value=""/> Dont call me again
</td>
</tr>
</g:else>
</g:each>
If you have 21 products and the dontCallMe
appears 21 times it means your condition is always false. 如果您有21种产品,并且
dontCallMe
出现21次,则表明您的情况始终为假。 What I would do is open up the code in the debugger
and verify that the object has the expected value and type that you expect. 我要做的是在
debugger
打开代码,并验证对象是否具有期望的值和期望的类型。
If your not using a debugger, add a new <td>
如果您不使用调试器,请添加新的
<td>
<td>
`${product.id}`
</td>
Also, if you don't have a debugger try something like this (havn't tried it myself): 另外,如果您没有调试器,请尝试以下操作(我自己尚未尝试过):
<td>
`${product.id?.getClass()}`
</td>
Finally, the code could be simplified as follows (please excuse typos): 最后,可以将代码简化如下(请原谅错字):
<g:each in="${products}" var = "product">
<tr>
<td>
<g:checkBox name="${product.id == '3' ? 'CallMe' : 'DontCallMe'}"
checked="${product.id == '3' ? 'true' : 'false'}"
value=""/>${product.id == '3' ? 'CallMe' : 'DontCallMe'}"
</td>
<td>
DEBUG: ${product.id} <br/>
DEBUG: ${product.id?.getClass()} <br/>
</td>
</tr>
</g:each>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.