聽起來就像你會想爲此使用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