Question

I am fairly new to Grails, and I would like to understand how to parse some form "params" with a controller, using a counter variable as an index?

I have a form:

<g:form controller="config" action="parseReports">
 <div>
 <g:each in="${allReports.toList()}" var="each" >
 <g:hiddenField name="reportName${allReports.indexOf(each)}" value="${each}" />
 </g:each>
 <g:hiddenField name="reportCountSize" value="${allReports.size()}" />

...

With some hidden variables.

In the controller, I have:

def reportCount = params.reportCountSize.toInteger()`

def reportCount0 = params.reportCount0.name.toString()`

This works fine.

I would like to know how to use a loop / index:

for (counter in 0..reportCount) {`
    def myReport = "${params}.reportCount${counter}.name.toString()}"`

....

I cannot find out how to get myReport to get the form value of params.reportCount0.name.toString(), but use the ${counter} variable?

Any help would be appreciated.

Thanks

Miles.

Was it helpful?

Solution

try this in cotroller.

for (counter in 0..reportCount - 1) {
       String report = params["reportName${counter}"]
       println "==${counter}===${report}====="
}

also you should use your each loop on gsp page like this..

<g:each in="${allReports.toList()}" var="each" status="i" >
    <g:hiddenField name="reportName${i}" value="${each}" />
</g:each>

Enjoy.

OTHER TIPS

params is a map, so you can treat it like that inside the Groovy String.

def params = [id:1234, id0:2345, id1:345]

for (counter in 0..1) {
  print "\n${params.get('id' + counter)}"
}

results in:

2345
345

However, I might advise you to check this out: Grails indexed parameters

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top