问题
I have tried for over a month get the correct rendering, binding and listing of selected checkboxes in grails (please see this link: How to get a list of checked boxes in grails).
I can get a list of checkboxes from the controller with this code: package frametest
import groovy.io.FileType
class Read_dirController {
def index() {
def list = []
def dir = new File("/Users/ironmantis7x/Documents/business/test_files/APITest/utils")
dir.eachFileRecurse (FileType.FILES) { file ->
list << file
}
list.each {
println it.name.replaceFirst(~/\.[^\.]+$/, '')
}
render(view: "index", model: [name:list.name])
params.list('fileName')
}
def uri() {
def api_link = String
render(view: "index", model: [name:url_link])
}
def displayForm() { }
/*def submitForm(String fileName) {
render params.list('fileName')
//request.getParameterValues('fileName')
}*/
def submitForm() {
def values = request.getParameterValues('fileName')
//render(view: "submitForm")
params.remove "_"
render (view: "submitForm", model: [params.list('values')])
}
}
that works fine.... I can display it in the gsp with this code:
<%@ page contentType="text/html;charset=UTF-8" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"/>
<meta name="layout" content="main"/>
<title>API Automation Test Page</title>
</head>
<body>
<div class="body">
<h2>Automation Test Page</h2>
<h4>Please choose test(s) below ...</h4>
<br>
<h3>Tests</h3>
<br>
<div>
<style>
.columns3 {
-webkit-column-count: 3;
-moz-column-count: 3;
column-count: 3; }
</style>
<g:form action="submitForm">
<ul class="columns3">
<g:each in="${name}" var="fileName" >
<g:checkBox value="${false}" name="${fileName}" id="${fileName.replaceFirst(~/\.[^\.]+$/, '')}" /> ${fileName.replaceFirst(~/\.[^\.]+$/, '')}<br>
</g:each>
</ul>
<br>
<br>
<g:submitButton name="Submit"/>
</g:form>
</div>
<br>
<br>
<%-- <h4>Quick Test</h4>--%>
<%-- Input API manually: <g:textField name="url_link" value="${api_link}" /> --%>
<%-- <g:submitButton name="qtest" value="go!" />--%>
<%-- <br>--%>
<%-- <br>--%>
API Output Response: <br>
<g:textArea name="name" rows="300" cols="3000" style="resize: none;"/>
</div>
</body>
</html>
The issue is this: when I select a set of checkbox(es), I get a blank list (here is the submit form gsp):
<%@ page contentType="text/html;charset=UTF-8" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"/>
<meta name="layout" content="main"/>
<title>API Automation Test Page</title>
</head>
<body>
<div class="body">
<h2>Automation Test Page</h2>
<h4>You have chosen the test(s) below.</h4>
<br>
<h3>Tests Chosen:</h3>
<br>
<div>
<%--<ul class="columns3">
<g:each in="${name}" var="fileName">
<g:checkBox value="${false}" name="${ 'fileName'}"/> ${fileName.replaceFirst(~/\.[^\.]+$/, '')}<br>
</g:each>
</ul>
--%>
<g:each in="${values}" var="tests">
<ul>
${'tests'} <br>
</ul>
</g:each>
<br>
<br>
</div>
</div>
</body>
</html>
when I check the binding through Chrome I get this list detail for the Form Data:
._DS_Store:
login._sh:
login.sh:on
Submit:Submit
I am kind of lost on what my error is. Can some one help me with a detailed explanation of the proper way to render a list of checkboxes and how to correctly render a list of selected checkboxes as well.
I am still new to grails. But I find it a very powerful and rich tool to do a lot of stuff.
Thanks!!
ironmantis7x
来源:https://stackoverflow.com/questions/37121551/how-to-properly-render-checkboxes-and-get-a-list-of-which-checkboxes-have-been-s