Grails 索引参数 [英] Grails indexed parameters
问题描述
我有一个具有 Integer seed
属性的 Team
对象列表.我想以一种形式一次性编辑所有团队的种子.我确定 Grails 支持索引参数,但我无法让它工作.
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>
控制器:
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
}
也就是说,我如何映射名为team[0].seed
、team[0].id
、team[1].seed<的参数/code>,
team[1].id
到列表?
That is, how do I map params named team[0].seed
, team[0].id
, team[1].seed
, team[1].id
to a List?
在 Stripes 中,您只需拥有一个 List
属性,它就会只是工作.我对 Grails 的期望也不低!;-)
In Stripes you can just have a List<Team>
property and it will just work. I expect no less from Grails! ;-)
预先感谢您的帮助.
推荐答案
我终于想出了如何在没有任何恶作剧的情况下做到这一点.
I finally figured out how to do this without any shenanigans.
忘记隐藏参数,只需在种子参数中使用团队 ID.在普惠制中:
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>
然后,在控制器中:
params.teams.seeds.each { teamId, seed ->
def team = Team.get(teamId.toInteger())
team.seed = seed.toInteger()
team.save()
}
redirect(action:list)
就像一个魅力.
这篇关于Grails 索引参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!