我从 Grails 开始,我不知道我应该如何面对以下用例。
该应用程序是关于体育结果预测的,所以我在我的域“Match”和“Prediction”中有一个 View ,用户可以在其中更新所有尚未播放的比赛的预测。
到目前为止,我已经在我的“PredictionController”中定义了一个方法,它搜索所有已经存在的必须玩的游戏的预测,并为任何日期比现在更高的新匹配生成新的预测实例。我已经为该方法创建了一个 View ,并且我正确地获得了我应该完成或更新的所有预测,并且我在我的 Controller 中定义了另一种表单提交的方法(所以我试图在与“创建”和“更新”脚手架方法的工作方式相同)。
我的问题是,如何访问我的 View 修改的所有预测?如何将所有预测发送到我的更新方法?它是否定义了一个包含所有集合的变量的隐藏字段?
这是我的 GSP View 中的表格:
<g:form action="savePredicctions">
<fieldset>
<g:each in="${predictions}">
<li>
<div>
${it.match.homeTeam}
<g:field name="${it.match}.homeGoals" type="number" value="${it.homeGoals}" />
</div>
-
<div>
<g:field name="${it.match}.awayGoals" type="number" value="${it.awayGoals}" />
${it.match.awayTeam}
</div>
</li>
</g:each>
</fieldset>
<fieldset class="submit">
<g:submitButton />
</fieldset>
</g:form>
最佳答案
您可以使用 command对象来存储预测的实例。
@Validateable
class PredictionCommand {
//data binding needs a non-null attribute, so we use ListUtils.lazyList
List<Prediction> predictions = ListUtils.lazyList([], FactoryUtils.instantiateFactory(Prediction))
}
在您看来,您需要控制列表的索引,并将 Prediction 的属性发送到 Controller :
<g:each in="${predictions}" status="i">
<g:textField name="predictions[$i].homeGoals" />
<g:textField name="predictions[$i].awayGoals" />
</g:each>
在您的 Controller 中,您可以使用
bindData()
绑定(bind)params
到你的命令:class CommandController {
def save() {
PredictionCommand command = new PredictionCommand()
bindData(command, params)
println command.predictions
}
}
关于grails - 在一个 View 中更新多个实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16159105/