听起来你会想为此使用 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>
我确定我搞砸了一些语法,你肯定需要对它进行一些重构才能使用你的代码。但至少你已经有了大致的想法。
要使用它们,请将它们作为 s 添加到数据库中ShippingOption。这是一种方法。
["fedex", "ups"].each { name ->
def so = new ShippingMethod(method: "ground", name: name )
so.save()
}
PS:您还可以动态呈现运输方式。
另请参阅:remoteFunction、g:select、模板和AJAX