これには AJAX を使用する必要があるようです。これを行う 1 つの方法は、テンプレートとドメイン オブジェクトの組み合わせを使用することです。
// 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>
私はいくつかの構文を台無しにしたと確信しています。コードを操作するには、これを少しリファクタリングする必要があります。しかし、少なくとも一般的な考え方は理解できました。
それらを使用するには、それらをShippingOptions としてデータベースに追加します。これを行う1つの方法を次に示します。
["fedex", "ups"].each { name ->
def so = new ShippingMethod(method: "ground", name: name )
so.save()
}
PS: 配送方法を動的にレンダリングすることもできます。
参照: remoteFunction、g:select、templates、およびAJAX