Grails: Load data on one ComboBox depending on another

前端 未结 3 1800
执念已碎
执念已碎 2020-12-06 22:00

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



        
3条回答
  •  被撕碎了的回忆
    2020-12-06 22:43

    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:

    
    
        
    
    

    And in your gsp with the select box on it:

    
    
    
    

    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

提交回复
热议问题