假设我有一个带有 GENERAL、AIR、GROUND 和 SEA 选项的组合框
<g:select name="group" from="${['GENERAL', 'AIR', 'GROUND', 'SEA']}" valueMessagePrefix="default.category" value="${tipoN}" />
然后是另一个组合框,它根据您选择的是 GENERAL、AIR、GROUND 还是 SEA 来加载某些信息。
假设GROUND
有 3 个选项,FedEx, USPS, DHL
,但是AIR
有完全不同的,AIRPLANE, JET, HOT AIR BALLOON
.
对方姓名<g:select>
应该是 "commodity"
我考虑过创建一个 javascript 文件并像处理 HTML 一样处理所有内容,但我做了一些谷歌研究,并没有我想的那么简单。
有谁知道最好的方法是什么??提前致谢!
FG
最佳答案
听起来您会想要为此使用 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
添加到数据库中。这是一种方法。
["fedex", "ups"].each { name ->
def so = new ShippingMethod(method: "ground", name: name )
so.save()
}
PS:您还可以动态呈现送货方式。
另请参阅:remoteFunction , g:select , templates , 和 AJAX
关于Grails:根据另一个在一个 ComboBox 上加载数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3770765/