gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 14:58:08 25 4
gpt4 key购买 nike

我发现人们尝试将多个复选框绑定(bind)到单个字段但失败了。这是我的例子..有没有更时髦的方法来做到这一点?

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 objectparams 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
}
}

Controller
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
...
}

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

正如詹尼斯在他们的评论中所说, multi-select对于这类数据可能会更好。
<g:select from="${counties}" name="counties" multiple="${true}"/>

然后你可以在你的命令对象中使用 Set<String> counties 绑定(bind)它。或使用 params.list('counties') 从您的参数中提取它.

关于Grails - 分隔字符串的多个复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32052204/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com