Grails:根据另一个 ComboBox 加载数据 [英] Grails: Load data on one ComboBox depending on another

查看:15
本文介绍了Grails:根据另一个 ComboBox 加载数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个包含 GENERAL、AIR、GROUND 和 SEA 选项的组合框

Let's say I have a combobox with the options GENERAL, AIR, GROUND, and SEA

<g:select name="group" from="${['GENERAL', 'AIR', 'GROUND', 'SEA']}" valueMessagePrefix="default.category" value="${tipoN}" />

然后是另一个组合框,它根据您选择 GENERAL、AIR、GROUND 还是 SEA 加载某些信息.

And then another combobox that loads certain information depending whether you select GENERAL, AIR, GROUND, or SEA.

假设 GROUND 有 3 个选项,FedEx、USPS、DHL,但是 AIR 有完全不同的选项,AIRPLANE、JET, 热气球.

Let's say GROUND has 3 options, FedEx, USPS, DHL, but AIR has complete different ones, AIRPLANE, JET, HOT AIR BALLOON.

另一个的名字应该是"commodity"

我想过创建一个 javascript 文件并将所有内容都像 HTML 一样处理,但我做了一些谷歌研究,并没有我想象的那么简单.

I thought about creating a javascript file and treating everything like HTML but I did some google research and is not as simple as I thought.

有谁知道这样做的最佳方法是什么?提前致谢!

Does anyone know what would be the best way to do this?? Thanks in advance!

FG

推荐答案

听起来您想为此使用 AJAX.一种方法是使用模板和域对象的组合:

Sounds like you'll want to use AJAX for this. One way you could do it is by using a combination of templates, and domain objects:

// 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 中带有选择框:

And in your gsp with the select box on it:

<!-- ... 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>

我确定我搞砸了一些语法,你肯定需要稍微重构一下才能使用你的代码.但至少你已经有了大致的想法.

I'm sure I've messed up some syntax, and you'll definitely have to refactor this a bit to work with your code. But at least you've got the general idea.

要使用它们,请将它们作为 ShippingOptions 添加到数据库中.这是一种方法.

And to use them, add them to the database as ShippingOptions. Here's one way to do it.

["fedex", "ups"].each { name ->
    def so = new ShippingMethod(method: "ground", name: name )
    so.save()
}

PS:您还可以动态呈现运输方式.

PS: You'd also be able to render the shipping methods dynamically, as well.

另见:remoteFunctiong:select, 模板AJAX

这篇关于Grails:根据另一个 ComboBox 加载数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆