Grails:复选框不会被设置为false [英] Grails: checkbox not being set back to false

查看:121
本文介绍了Grails:复选框不会被设置为false的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发Grails(1.0.4)应用程序,我想在网格视图的单个页面上编辑集合的集合。我只依靠Spring MVC的索引参数处理工作得很好,除了一件事:



布尔值(或者对于布尔值)网格可以通过复选框设置,但不会取消设置,即当我选中复选框并更新时,该值设置为true,但之后当我再次编辑时,取消选中复选框并更新,它仍然为真。



这是复选框的GSP代码:

 < g:复选框名称= tage [$ {indexTag}] .zuweisungen [$ {indexMitarb}]。fixiertvalue =$ {z.fixiert}/> 

这是生成的HTML:

 < input type =hiddenname =tage [0] .zuweisungen [0] ._ fixiert/> 
< input type =checkboxname =tage [0] .zuweisungen [0] .fixiertchecked =checkedid =tage [0] .zuweisungen [0] .fixiert/>

我找到了 Grails bug ,它描述了这种效应,但它在1.0.2中被标记为固定的,并且在那里描述的问题机制(在隐藏字段名称中加下划线错误的地方)在我的情况下不存在。



任何想法可能是什么原因?

解决方案

这是一个名为Julius Huang的人在grails-user邮件列表中提出的解决方案。它是可重用的,但依赖于JavaScript来填充一个隐藏的字段,其中包含一个未选中的复选框的false响应,但遗憾的是,这个复选框不会发送。 $ b




自定义TagLib取消选中该框(true - > false)时,我会让GSP发送false。默认情况下,checkBox不发送任何东西当
取消选中时,所以我使用checkBox作为
事件处理程序,但是发送隐藏字段



paramsin Controller can在没有任何
修改的情况下处理
false - >true。例如。在控制器中,所有内容都保持

GSP中的自定义标签使用情况(示例usedfunc_F为true), p>

 < jh:checkBox name =surveyList [$ {i}]。usedfunc_Fvalue =$ {survey.usedfunc_F} >< / JH:复选框> 

这是标记生成的内容,

 < input type =hiddenname =surveyList [#{i}]。usedfunc_Fid =surveyList [#{i}]。usedfunc_Fvalue =false/ > 
< input type =checkboxonclick =jhtoggle('surveyList [#{i}]。usedfunc_F')checked =checked/>

Javascript

 < script type =text / javascript> 
函数jhtoggle(obj){
var jht = document.getElementById(obj);
jht.value =(jht.value!='true'?'true':'false');
}
< / script>


I am developing a Grails (1.0.4) app where I want to edit a collection of collections on a single page in a grid view. I got it to work quite well depending only on the indexed parameter handling of Spring MVC, except for one thing:

boolean (or, for that matter, Boolean) values in the grid can be set via checkbox, but not unset, i.e. when I check the checkbox and update, the value is set to true, but afterwards when I edit again, uncheck the checkbox and update, it remains true.

This is the GSP code of the checkbox:

<g:checkBox name="tage[${indexTag}].zuweisungen[${indexMitarb}].fixiert" value="${z.fixiert}" />

And this is the HTML that is generated:

<input type="hidden" name="tage[0].zuweisungen[0]._fixiert" />
<input type="checkbox" name="tage[0].zuweisungen[0].fixiert" checked="checked" id="tage[0].zuweisungen[0].fixiert"  />

I've found a Grails bug that describes exactly this effect, but it's marked as fixed in 1.0.2, and the problem mechanism described there (underscore in hidden field name is put in the wrong place) is not present in my case.

Any ideas what could be the reason?

解决方案

This is the solution a guy named Julius Huang proposed on the grails-user mailing list. It's reusable but relies on JavaScript to populate a hidden field with the "false" response for an unchecked checkbox that HTML unfortunately does not send.

I hack GSP to send "false" when uncheck the box (true -> false) with custom TagLib.

By default checkBox send nothing when uncheck, so I use the checkBox as event handler but send hidden field instead.

"params" in Controller can handle "false" -> "true" without any modification. eg. Everything remain same in Controller.

The Custom Tag Usage in GSP (sample usedfunc_F is "true"),

<jh:checkBox name="surveyList[${i}].usedfunc_F" value="${survey.usedfunc_F}"></jh:checkBox>

Here is what the Tag generate,

<input type="hidden" name="surveyList[#{i}].usedfunc_F" id="surveyList[#{i}].usedfunc_F" value="false" />
<input type="checkbox" onclick="jhtoggle('surveyList[#{i}].usedfunc_F')" checked="checked" />

The Javascript

<script type="text/javascript">
function jhtoggle(obj) {
   var jht = document.getElementById(obj);
   jht.value = (jht.value !='true' ? 'true' : 'false');
}
</script>

这篇关于Grails:复选框不会被设置为false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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