views:

992

answers:

3

I have a list of Team objects that have an Integer seed property. I want to edit all the teams' seeds at once, in a single form. I'm sure that Grails supports indexed parameters, but I can't get it to work.

Here is what I have, and it works but I'm jumping through way too many hoops and there's gotta be a better way.

gsp:

<g:form action="setSeeds">
...
  <g:each in="${teams}" status="i" var="team">
    <input type="hidden" name="teams[${i}].id" value="${team.id}">
    <input type="text" size="2" name="teams[${i}].seed" value="${team.seed}">
  </g:each>
</g:form>

controller:

def setSeeds = {
  (0..<30).each { i ->
    def team = Team.get(Integer.parseInt(params["teams[${i}].id"]))
    team.seed = Integer.parseInt(params["teams[${i}].seed"])
  }
  redirect(action:list)
}

Isn't that awful? Way too much noise. How can I do something along the lines of:

params.teams.each { t ->
  def team = Team.get(t.id)
  team.seed = t.seed
}

That is, how do I map params named team[0].seed, team[0].id, team[1].seed, team[1].id to a List?

In Stripes you can just have a List<Team> property and it will just work. I expect no less from Grails! ;-)

Thanks in advance for your help.

A: 

Not sure if this helps but you could use a closure with it like:

<g:each in="${teams}">
     <p>id: ${it.id}</p>
     <p>seed: ${it.seed}</p>
</g:each>

You could probably build a list from this instaed of outputting html. or build your form with it.

Sean A.O. Harney
I'm already using a closure to build a form. The question is, how do I retrieve the indexed parameters in the controller to build a list?
Frederic Daoud
+4  A: 

params is more than a regular Map, it's a GrailsParameterMap that automatically builds up sub-Map structures based on splitting the parameter names by '.'. You might take advantage of this by using the following gsp:

<g:form action="seed">
 <g:each in="${teams}" status="i" var="team">
   <input type="hidden" name="teams.${i}.id" value="${team.id}">
   <input type="text" size="2" name="teams.${i}.seed" value="${team.seed}">
  </g:each>
 <g:submitButton name="update" value="Update" />
</g:form>

NB: there's no [] in the name attributes. The controller is now pretty simple using some black Grails magic :

log.error "params = ${params}"
params.teams.findAll {k,v -> !k.contains(".")}.each { k,v ->
       bindData(Team.get(v.id), v)
}

The first operation findAll filters out all parameters with a dot inside. The rest is a map of maps holding the row id in k and the id and seed in v.

I hope this answers your question.

Stefan
+1  A: 

I finally figured out how to do this without any shenanigans.

Forget about the hidden parameter and just use the team ID in the seed parameter. In the GSP:

<g:form action="setSeeds"> 
... 
  <g:each in="${teams}" var="team"> 
    <input type="text" size="2" name="teams.seeds.${team.id}"
      value="${team.seed}"> 
  </g:each> 
</g:form>

Then, in the controller:

params.teams.seeds.each { teamId, seed ->
  def team = Team.get(teamId.toInteger())
  team.seed = seed.toInteger()
  team.save()
}
redirect(action:list)

Works like a charm.

Frederic Daoud