我希望我的选择下拉菜单自动Grails的网站填充阿贾克斯 [英] I want my selects dropdowns to auto populate with Ajax in Grails website

查看:195
本文介绍了我希望我的选择下拉菜单自动Grails的网站填充阿贾克斯的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Groovy中创建Grails的一个选择下拉扶养。我想我的新用户从选择的下拉列表中选择自己的国家,provstate和electrolDistrict。我已经试过了ajaxdependancyselection插件,但我只能拿到国内露面。该ProvState和ElectoralDistricts不会出现在下拉列表中。也许我应该使用对象图生成器或节点生成器来代替。任何建议将是巨大的。谢谢。

I'm trying to create a "select dependancy dropdown" in Groovy Grails. I want my new users to choose their country, provstate and electrolDistrict from select dropdowns. I've tried the ajaxdependancyselection plugin, but I can only get the country to show up. The ProvState and ElectoralDistricts don't appear in the dropdown. Maybe I should use object graph builder or node builder instead. Any suggestions would be great. thanks.,

推荐答案

嘿,我把插件在一起,因此,如果您需要帮助它让我知道,对不起,还没有看到这个问题到现在。

Hey I put the plugin together so if you need help with it let me know, sorry had not seen the question until now.

如果你可以分享这些3类域类信息:  国家,provstate和electrolDistrict然后我可以帮你放在一起有什么需要做的事情。

if you can share your domain class info for these 3 classes: country, provstate and electrolDistrict then I Can help you put together what needs to be done.

首先什么的

    country  : String name, static hasMany = [states:provestate]
    provstate: String ???, 
static belongsTo = [ Country ] 
or ? static belongsTo = [ Country:country ],
 static hasMany= [district: electrolDistrict] 

    electrolDistrict : 
 String ???,
 static belongsTo = [ provstate ] 
or ? static belongsTo = [ state:provstate ],

还有: https://github.com/vahidhedayati/ajaxdependancyselectexample

取下的国家观一看,也有一些例子,扩大了3个选项。

Take a look under country view and there are some examples there expanding over 3 selections.

我个人虽然这种解决方案一切运作良好,我发现使用remoteFunction要快过密集的桌子,

Personally although this solution all works well I found using remoteFunction to be faster over dense tables,

下面是我想我需要将其纳入插件的一些方法的另一种方法,它只是基于两个选择框,但你可以简单地只是扩大第二选择第三加入了遥控功能,以它。 ......

Here is the alternative method which I think I need to incorporate into the plugin at some method, it is only based on two select boxes but you can simply just expand the second selection to a third by adding a remote function to it.....

<div class="smallform">




<div id=SelectCountry class="fieldcontain ${hasErrors(bean: furnitureDonationInstance, field: 'country', 'error')} required">
    <label for="country">
        <g:message code="testcity.country.label" default="Choose Country" />
        <span class="required-indicator">*</span>
    </label>
    <g:select id="country" name="country" from="${com.myprofessions.radpost.Globe.list()}" optionKey="iso"  optionValue="country" required="" value="${params.country}" class="many-to-one"
        noSelection="['null': 'Please choose Country']"
        onchange="${remoteFunction (
        controller: 'selector',
        action: 'findCities',
        params: "'id=' + this.value",
        update: 'cityId'
    )}"
    />
</div>


<div id=SelectCity1 class="fieldcontain ${hasErrors(bean: furnitureDonationInstance, field: 'City', 'error')} required">
    <label for="country">
        <g:message code="testcity.city.label" default="Choose Country" />
        <span class="required-indicator">*</span>
    </label>
    <div id="cityId">
<g:select name="city" id="cityId1" optionKey="id" optionValue="city" from="[]" 
noSelection="['null': 'Please choose Country 11']" />
</div>
</div>

现在有一个单独的模板名为 _cities.gsp ,其中包含当由控制器我被称为选择的findCities行动呼吁将取代cityId格内的内容

Now having a seperate template called _cities.gsp which contains and will replace cityId div inner content when called by findCities action of a controller I got called selector

<g:select name="city" from="${cities}" optionValue="city" optionKey="id" noSelection="['null': 'Please choose city']"/>

这是要在其中添加其他的onChange像上面并重复此过程为您的下一个项目

This is where you would add another onChange like above and repeat the process for your next item

SelectorController:

SelectorController:

def findCities() { 
            def s=params.id
            String domclass1= (s.substring(0,1).toUpperCase())
            String domclass2=s.substring(1,s.length())
            String domclass=domclass1+domclass2.toLowerCase()
            Class domainClass = grailsApplication?.domainClasses.find { it.clazz.simpleName ==domclass+'Cities' }?.clazz
            def cities=loadCities(domainClass)
            render(template: 'cities', model:  [cities: cities])
        }
@Cacheable("RADPOST-CITY")
        def loadCities(def domainClass) {
            if (domainClass) {
            def cities=domainClass?.findAll()
            return cities
            }
        }

其中最后的结果放在一起,并将其返回到城市模板

Which finally puts the results together and returns it to cities template

我呼吁domainClass等COS我用它从另一个插件,这样你就可以取代所有这一切做provstate?.findAll()等。

I have called domainClass etc cos I am using it from within another plugin so you could replace all of that do provstate?.findAll() and so forth.

在速度上的差异,我相信是到如何ajaxdependancyselection插件捕捉每一个项目,使用JavaScript来重新创建中选择下降选项元素下来。

The speed difference I believe is down to how the ajaxdependancyselection plugin captures each item and using javascript to recreate the options element within the select drop down.

哦对了是通过了ISO它,然后添加到domclass +城市的城市,并创建AoCities或AuCities等等,这样就看出来它和修改,根据你的实际domainClass或提及删除所有搜索类,只是直接调用它

Ohh by the way it is passing the iso which it then adds to domclass+'Cities' cities and creates AoCities or AuCities and so forth so that do watch out for it and amend according to you actual domainClass or as mentioned remove all the searching for the class and just call it directly

这篇关于我希望我的选择下拉菜单自动Grails的网站填充阿贾克斯的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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