Grails - 多个复选框分隔字符串

Grails - multiple checkboxes to delimited string

我发现人们尝试过将多个复选框绑定到一个字段,但都失败了。这是我的例子.. 可以有更多 groovy 的方法来做到这一点吗?

index.gsp

     <html>
 <head>
<meta name="layout" content="main"/>

<title>Dat multiple check box to string tho</title>
 </head>
 <body>

<g:form action="submitFormIndex">
    Applicant Name<g:textField name="name" required="" value=""/>
    <br>


    Albany: <g:checkBox name="albany" value="albany" checked="false"  />  <br> 
    Allegany: <g:checkBox name="allegany" value="allegany" checked="false"  />  <br> 
    Bronx: <g:checkBox name="bronx" value="bronx" checked="false"  />  <br> 
    Broome: <g:checkBox name="broome" value="broome" checked="false"  />  <br> 
    Cattaraugus: <g:checkBox name="cattaraugus" value="cattaraugus" checked="false"  />  <br> 
    Cayuga: <g:checkBox name="cayuga" value="cayuga" checked="false"  />  <br> 
    Chautauqua: <g:checkBox name="chautauqua" value="chautauqua" checked="false"  />  <br> 
    Chemung: <g:checkBox name="chemung" value="chemung" checked="false"  />  <br> 
    Chenango: <g:checkBox name="chenango" value="chenango" checked="false"  />  <br> 
    Clinton: <g:checkBox name="clinton" value="clinton" checked="false"  />  <br>       
    Onondaga: <g:checkBox name="onondaga" value="onondaga" checked="false" /> <br> <br>

    <g:submitButton name="Submit"/>
</g:form>
 </body>
 </html>

TestFormController.groovy

 package countiesselectme

 class TestFormController {

def index() {}

def submitFormIndex(String name, String albany, String allegany, String bronx, String broome, String cattaraugus, String cayuga, String chautauqua, String chemung, String chenango, String clinton, String onondaga) {
    def cleanDemNulls = (["${albany}", "${allegany}", "${bronx}", "${broome}", "${cattaraugus}", "${cayuga}", "${chautauqua}", "${chemung}", "${chenango}", "${clinton}", "${onondaga}"]- 'null')
    new Applicant(name:"${name}" , countiesWillingToWork:cleanDemNulls).save()

    redirect (controller: "Applicant" , action:'index')
 }  


 }

您可以使用 command object or the params object 检索复选框值。

普惠制

%{-- Value can be some gsp boolean var, this example uses constants --}%
<g:checkbox name="albany" value="${true}"/>
<g:checkbox name="allegany" value="${false}"/>

命令对象

@Validateable
def TestFormCommand {
    Boolean albany
    Boolean allegany
    ...

    def countiesList(){
        def counties = []
        if(albany) counties << "albany"
        if(allegany) counties << "aalleganybany"
        ...
        return counties
    }
}

控制器

def action(TestFormCommand cmd){
    // Using command object
    def albany = cmd.albany
    def allegany = cmd.allegany

    // Using params
    def albanyParam = params.boolean('albany');
    def alleganyParam = params.boolean('allegany');

    // Use the utility method in the command object
    def countiesWillingToWork = cmd.countiesList
    ...
}

这也意味着您不需要像在当前代码中那样删除空值。

正如 Giannis 在他们的评论中所说,multi-select 可能更适合此类数据。

<g:select from="${counties}" name="counties" multiple="${true}"/>

然后您可以使用 Set<String> counties 在您的命令对象中绑定它,或者使用 params.list('counties').

从您的参数中提取它