复选框列表 [英] a list of checkboxes

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

问题描述

我有两个域类

  class Contract {
字符串编号
static hasMany = [语句:语句]
}

类语句{
字符串代码
static hasMany = [contracts:Contract]
}

我想在gsp中显示所有可用的语句,每个语句旁边都有一个复选框,允许用户选择哪些语句适用于合同。例如:

  []语句代码1 
[]语句代码2
[]语句代码3

我从此开始:

 < g:each in =$ {Statement.list()}var =statementstatus =i> 
< label for =语句[$ {i}]> $ {statement.code}< / label>
< / g:每个>

但我只是无法获取到控制器的检查语句列表( null 元素在列表中,有重复的语句......)。

任何想法如何实现?

解决方案

这是可能的,但它确实需要一些破解。首先,每个复选框必须具有相同的名称,语句:

 < g:each in =$ {org .example.Statement.list(sort:'id',order:'asc')}var =statementstatus =i> 
< label for =语句> $ {statement.content}< / label>
< / g:每个>

其次,在控制器中,您必须在绑定之前删除_statements属性:

  def contract = Contract.get(params.id)
params.remove_statements
bindData合约,params
contract.save(failOnError:true)

复选框支持不是专为这个用例,因此需要一个黑客。多选列表框是通常用于此类场景的列表框。


I have two domain classes

class Contract {
    String number
    static hasMany = [statements:Statement]
}

class Statement {
    String code
    static hasMany = [contracts:Contract]
}

I would like to show all statements available in my gsp with a checkbox next to each, allowing the user to choose which statements are applicable to the contract. So something like:

[ ] Statement Code 1
[ ] Statement Code 2
[ ] Statement Code 3

I started off with this:

<g:each in="${Statement.list()}" var="statement" status="i">
    <g:checkBox name="statements[${i}].id" value="${statement.id}" checked="${contractInstance.statements.contains(statement.id)}" />
    <label for="statements[${i}]">${statement.code}</label>
</g:each>

But i just cannot get a list of checked statements to the controller (there are null elements in the list, there are repeated statements...).

Any idea how to achieve this?

解决方案

This is possible, but it does require a bit of a hack. First off, every checkbox must have the same name, "statements":

<g:each in="${org.example.Statement.list(sort: 'id', order: 'asc')}" var="statement" status="i">
    <g:checkBox name="statements" value="${statement.id}" checked="${contract.statements.contains(statement)}" />
    <label for="statements">${statement.content}</label>
</g:each>

Second, in the controller you have to remove the "_statements" property before binding:

def contract = Contract.get(params.id)
params.remove "_statements"
bindData contract, params
contract.save(failOnError: true)

The check box support hasn't been designed for this use case, hence the need for a hack. The multi-select list box is the one typically used for this type of scenario.

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

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