绑定多个复选框以在Grails中列出 [英] Binding multiple checkboxes to list in Grails

查看:62
本文介绍了绑定多个复选框以在Grails中列出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看似简单,但显然很难.将一系列(有限的已知长度)复选框绑定到grails命令对象上的布尔值列表.当然,应该可以填充"命令对象以使用先前选择的值来还原视图.

It seems simple but is apparently very hard. Bind a (finite and known length) series of checkboxes to a list of booleans on a grails command object. It should of course be possible to "populate" the command object to restore the view, with the previous selected values.

例如,我有一个grails网络流.首先将4个复选框绑定到布尔列表中的一个条目.它切换到下一个状态并打印值-例如[true,true].向后浏览,所有复选框均为空(这很有意义-4个复选框和只有两个值).所以它应该是例如[false,true,false,true].

E.g I have a grails webflow. It start out by binding 4 checkboxes to an entry in a boolean list. It switches to next state and prints the values - e.g. [true,true]. Navigate back, and all the checkboxes are empty (which makes perfectly sense - 4 checkboxes and only two values). So it should rather be e.g. [false, true, false, true].

无论我做什么,我似乎都无法做到这一点.在春季,这很容易,只需将复选框绑定到value [0] .. [3] ...为什么在Grails中这么简单的东西就这么难?!

No matter what I do, I cant seem to make this happen. In spring it would be easy, simply bind the checkbox to value[0]..[3]... Why is somehthing this simple so hard in Grails?!

请帮助,这真的会让我高兴!

Please help, it would really make my day!

示例命令对象:

class TestCmdObj {    
    List<Boolean> boolListOne = []
}

控制器中的绑定方法示例(在流程动作中执行闭包):

Example binding method in controller (closure executed in flow action):

private def doBindAndValidateBoolList = {       
    bindData(flow.testCmdObj, params, [include:['boolListOne']])
}

到目前为止,一切都很好.当显示流中的下一页时,此方法实际上起作用,testCmdObj.boolListOne对于选中的复选框显示为true..

So far so good. This actually works when displaying next page in flow, the testCmdObj.boolListOne displays true for the checkboxes checked..

GSP代码很简单,并使用:

The GSP code is simple, and uses:

<g:checkBox name="boolListOne"/>
<g:checkBox name="boolListOne"/>
<g:checkBox name="boolListOne"/>
<g:checkBox name="boolListOne"/>

在下一个视图中,我可以打印出$ {testCmdObj.boolListOne},并打印出与我检查的一样多的"true"值.(例如[true,true])如果我向后浏览,则该复选框是空的...我又能理解,因为它们都具有相同的名称...

In the next view, I can print out ${testCmdObj.boolListOne} and it prints out as many "true" values, as I checked of..(e.g. [true, true]) If I navigate back, the checkboxes are emtpy... Which, again, I can understand, since they all have the same name...

推荐答案

希望最终确定上述建议/评论之间的所有内容.

Wanted to finalise all in between the above suggestions / comments.

  1. 如果您声明复选框的迭代并调用它们

<g:each in="someIteration" var="a">
  <g:checkBox name="something" value="${a.someCondition}" 
  checked="${a.someCondition}" />
</g:each>

您的控制者将以 String[L] 有点像String[] something的验证对象 尽管您仍将其声明为List something=[].

Your controller in grails will pick up this up as String[L] A bit like a validation object of String[] something Although you would still declare this as List something=[].

只要最终用户选择了多个复选框,此方法也将起作用.并且您的验证对象将在此时List something=[]

This also appears to work so long as the end user has selected more than 1 checkbox. and your validation object would pickup at this point List something=[]

上面的问题是,如果选择1,则参数以string而不是list

The issue with above is that if 1 selection is made the param is received more as a string than a list

如果您不依赖于对象的自动绑定,那么上面的操作没什么问题,

Nothing wrong with above if you are not relying on auto binding of objects since you would do

List myList = params.list('something')

这时,grails会将1或倍数转换为列表.但这不会将单个复选框绑定到验证列表元素.

at this point grails will convert whether 1 or multiple into the list. But this won't bind a single checkbox to a validation list element.

要从验证的角度解决此问题,就是将其声明为List something=[]

To fix this from a validation point of view would be to declare it as List something=[]

<g:each in="someIteration" status="i" var="a">
  <g:checkBox name="something[${i}]" value="${a.someCondition}" 
   checked="${a.someCondition}" />
</g:each>

现在,无论选择1个复选框还是多个复选框,它都将在something列表下(如['null','whatever','null','null'])被拾取.

Now this will be picked up under something list like ['null','whatever','null','null'] regardless of 1 selection or multiple check box selection.

使用status字段并将名称从something重命名为something[${i}]

It is to use the status field and rename name from something to something[${i}]

这篇关于绑定多个复选框以在Grails中列出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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