Grails复选框 [英] Grails checkbox
问题描述
在关联类中绑定Boolean属性时遇到了问题。如果我选中复选框(好),但是空,如果checbox没有被选中,属性设置为 true 。
我知道HTML复选框的问题。我知道为什么在params中发送_fieldName,但是这个_fieldName不会将我的布尔属性设置为 false 。
class Person {
String title
List< Group> groups = new ArrayList()
static hasMany = [groups:Groups]
}
class Group {
String title
Boolean isHidden
static belongsTo = Person
}
$ b $ class PersonController {
def form = {
def person = new Person()
person.groups.add(new Group())
return [person:person]
}
def handleForm = {
def person = new Person (params)
println person.groups [0]
}
}
< g:submitButton name =savevalue =Save/>
< / g:表格>
如果选中复选框:
[isHidden:on,title:a, _isHidden:]
println person.groups [0] // true
如果我不勾选复选框:
[title:a,_isHidden:]
println person.groups [0] // null
非常感谢您的帮助
Tom
对不起,我搜索了这个网页,但没有得到实际的我的麻烦。
我正确复选框标记。感谢gid的帮助,现在它也适用于关联。
来源:
http://grails.org/doc/latest/ref/Tags/checkBox.html#
if(value == null)value = false
out<< < input type = \hidden \name = \_ $ {name} \/>< input type = \checkbox \name = \$ {name} \
if(value&& checked){out<< 'checked =checked'}
到:
if(value == null)value = false
def begin = name.lastIndexOf('。')+1
def tail = name.substring(begin);
out<< < input type = \hidden \name = \$ {name.replace(tail,_+ tail)} \/>< input type = \checkbox \ name = \$ {name} \
if(value&& checked){out<< 'checked =checked'}
I have trouble with binding Boolean property in association classes. Property is set to true if I check checkbox (good), but is null if checbox is not checked.
I know the problem with HTML checkbox. I know why is send "_fieldName" in params, but this "_fieldName" dont set my boolean property to false.
class Person{
String title
List<Group> groups = new ArrayList()
static hasMany = [groups: Groups]
}
class Group{
String title
Boolean isHidden
static belongTo = Person
}
class PersonController{
def form = {
def person = new Person()
person.groups.add( new Group() )
return ["person": person]
}
def handleForm = {
def person = new Person( params )
println person.groups[0]
}
}
<g:form action="save">
<g:textField name="title" value="${person?.title}" />
<g:textField name="groups[0].title" value="${person?.groups[0]?.title}"/>
<g:checkBox name="groups[0].isHidden" value="${person?.groups[0]?.isHidden}" />
<g:submitButton name="save" value="Save" />
</g:form>
If I check checkbox:
[isHidden:on, title:a, _isHidden:]
println person.groups[0] //true
If I don check checkbox:
[title:a, _isHidden:]
println person.groups[0] //null
Thank a lot for help
Tom
I am sorry, I searched this web, but did not get actual info for my trouble.
I correct checkbox tag. Thanks to gid help, now it work with association too.
from source:
http://grails.org/doc/latest/ref/Tags/checkBox.html#
if (value == null) value = false
out << "<input type=\"hidden\" name=\"_${name}\" /><input type=\"checkbox\" name=\"${name}\" "
if (value && checked) { out << 'checked="checked" ' }
to:
if (value == null) value = false
def begin = name.lastIndexOf('.') +1
def tail = name.substring( begin);
out << "<input type=\"hidden\" name=\"${name.replace( tail, "_" + tail )}\" /><input type=\"checkbox\" name=\"${name}\" "
if (value && checked) { out << 'checked="checked" ' }
这篇关于Grails复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!