Grails:根据另一个组合框加载数据

发布于 2024-09-24 23:05:07 字数 586 浏览 10 评论 0原文

假设我有一个包含选项 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气球。

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

我想过创建一个 javascript 文件并将所有内容都视为 HTML,但我做了一些谷歌研究,并且是并不像我想象的那么简单。

有谁知道最好的方法是什么?提前致谢!

FG

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}" />

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

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

The name of the other <g:select> should be "commodity"

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

救赎№ 2024-10-01 23:05:07

听起来您会想为此使用 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:您还可以动态呈现运输方式。

另请参阅:remoteFunctiong:select, 模板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 ])
    }
}

And the template:

<!-- grails-app/views/_shippingList.gsp -->
<g:each var="opt" in="${commodityList}">
    <option value="${opt.name}">${opt.name}</option>
</g:each>

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.

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: You'd also be able to render the shipping methods dynamically, as well.

See also: remoteFunction, g:select, templates, and AJAX

绅刃 2024-10-01 23:05:07

我会考虑重新设计你的用户界面并改变流程。
您所描述的下拉依赖项表明表单可能应该被分割,并且采用“类似向导”的解决方案将产生一个更加用户友好和可靠的解决方案,该解决方案也可以在没有 JavaScript 的情况下工作。

I would consider re-designing your UI and changing the flow.
A drop-down dependency that you are describing suggests the form should probably be split and adopting a 'wizard-like' solution will result in a more user-friendly and solid solution that will work also without JavaScript.

溺ぐ爱和你が 2024-10-01 23:05:07

我在这里有一个使用 AngularJS 和 Grail 的工作示例:

http://wordpress.transentia.com.au/wordpress/2013/12/24/keeping-up-with-the-joneses/

(如果这不合适,那么“风格”很抱歉,但是我认为发布数百行代码和废话也不合适)。

I have a worked example using AngularJS and Grail here:

http://wordpress.transentia.com.au/wordpress/2013/12/24/keeping-up-with-the-joneses/

(apologies if this is not appropriate SO 'style', but I dont' think that posting 100s of lines of code and verbiage is appropriate, either).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文