听起来就像你会想为此使用AJAX。你可以做到这一点的方法之一是使用的模板组合,和域对象:
// grails-app/domain/ShippingOption.groovy
class ShippingOption = {
String method, // can be 'ground', 'sea', 'air', or 'general'
name // can be 'fedex', 'ups', etc.
def options = {
def meth = params.method ?: "general"
def comList = ShippingOption.findByMethod(meth)
render(template:"shippingList", model: [ commodityList: comList ])
}
}
而且模板:
<!-- grails-app/views/_shippingList.gsp -->
<g:each var="opt" in="${commodityList}">
<option value="${opt.name}">${opt.name}</option>
</g:each>
而且在与它的选择框您的GSP:
<!-- ... other stuff is before here ... -->
<g:select name="method" from="${['GENERAL', 'GROUND', 'SEA', 'AIR']}"
onchange="${remoteFunction(action:'options', update:'commodity',
params:''method=' + this.value')}" />
<select id="commodity"></select>
我确定我搞砸了一些语法,你一定要重构这一点来处理你的代码。但至少你已经有了一般想法。
要使用它们,请将它们作为ShippingOption
s添加到数据库中。这是一种方法。
["fedex", "ups"].each { name ->
def so = new ShippingMethod(method: "ground", name: name)
so.save()
}
PS:您还可以动态呈现送货方式。
参见:remoteFunction,g:select,templates和AJAX
来源
2010-09-22 16:05:56
Pat
但我在哪里给每个“方法”的选项?我明白了,我只是有点失落。像我在哪里给每个选项? – randomizertech 2010-09-22 20:32:00
查看编辑答案。 – Pat 2010-09-23 01:42:41
最后一段代码应该去哪里?在控制器? – randomizertech 2010-09-24 13:50:58