1

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

tim_yates
  • 167,322
  • 27
  • 342
  • 338
Dave
  • 11
  • 4

1 Answers1

1

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

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.

firstly whats their dependancy like between

    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 ],

There is also : https://github.com/vahidhedayati/ajaxdependancyselectexample

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

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>

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']"/>

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

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

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.

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.

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

V H
  • 8,382
  • 2
  • 28
  • 48
  • Wow, do I ever feel dumb for not checking this post after writing it over 2 years ago. Sorry about that. Luckily I still have the same problem and know by the replys here that I'm going to figure select dropdown dependency thing. I'll post my domain classes and registration form below. I'm trying to make the user select their country, province,and 4 political jurisdictions. FederalRiding, ProvincialRiding, CivicWard, and FirstNation. The relationships are like this: Country has 1 child: Province. Province has 4 children. When the the user registers they must choose their 6 locations. – Dave Jan 20 '15 at 03:25
  • So I want the province dropdown to repopulate when the country is selected, and I want the 4 jurisdictions to repopulate when the province is selected. – Dave Jan 20 '15 at 03:34
  • class Province { String name Country country static hasMany = [ firstNations: FirstNation, ridings:Riding, provridings:Provriding,civicridings:Civicriding ] – Dave Jan 20 '15 at 03:37
  • class FirstNation { String name Province province static hasMany = [ profiles:Profile, candidates: Candidate ] – Dave Jan 20 '15 at 03:39
  • class Provriding { String name Province province static hasMany = [ profiles:Profile, candidates: Candidate ] – Dave Jan 20 '15 at 03:40

  • Username:

    Password:

    Confirm password:


    Country:
    – Dave Jan 20 '15 at 03:47
  • Fairly easy to do if I understand correctly province needs to make use of domain2 domain3..domain5. In my got hub download testad lookup up views my continent. There should be example of multi elements. Sorry on the phone raise issue on a jade pendant and I'll help u through it. Also there is a new plug in unreleased boselecta secure socket dependency selection which may be good in your situation – V H Jan 20 '15 at 08:15
  • so your primary would be the default like this https://github.com/vahidhedayati/testad/blob/master/grails-app/views/myContinent/example3.gsp#L59 and secondary being your province something like this https://github.com/vahidhedayati/testad/blob/master/grails-app/views/myContinent/multidomainexample.gsp#L177 but you would expand domain4 to domain5 and for each one define each relationship – V H Jan 20 '15 at 09:17
  • Great!, thanks Vihid, I've looked at your solution and think I would like to try it first, and then look at boselecta . I don't see any controller code for action=example5. Is your solution in a package? Thanks a a lot for helping... I'll play around with the code later tonight and see if I can get it working.....I'm sure I'll have some questions. Thanks, Dave – Dave Jan 20 '15 at 20:30
  • Hey Dave no worries, if you like raise an issue on ajaxdependancyselection and I can help you out on the issue itself so it saves trying do it here. The action=example5 is a simple GSP page that just renders the params passed to it - which be the ID's of the selection. Boselecta isn't released as yet but hopefully soon. Have a read and it may not be for you (if you have no idea of your userbase) since rather than passing stuff using ajax it does it discretely via socket client/server. https://github.com/vahidhedayati/grails-boselecta-plugin – V H Jan 20 '15 at 21:06
  • Great, so I will work with ajaxdependancyselection again. i almost had it working before. I still have my code for that. I'll let you know when I get stuck...! thanks – Dave Jan 21 '15 at 04:58
  • Hi Vahid, I've tried to get the second drop down to populate but to no avail. I've pasted the address above for the registration page. I've also added the code I've used. Can you take a look at it. Maybe check the source code too. Maybe I'm missing something obvious. I'm not suyre why its not working. Thanks a lot. No rush. – Dave Jan 23 '15 at 04:58
  • can u also add the first two domain classes as well ? or at least just those fields package name etc so just the top bits and really name id definitions and your bind between two. – V H Jan 23 '15 at 05:06
  • also noticed the moment I go to select anything loads of unauthorised errors are returned to :8080/login/authAjax Request Method: GET Status Code: HTTP/1.1 401 Unauthorized ... unsure if this is actually the reason.. as in it tries to carry out an action and it hits the authorisation right... if possible try disabling auth all together then work out what is going where that is causing this – V H Jan 23 '15 at 05:11
  • Actually it is your security :) http://stimulator.bigideabase.co:8080/autoComplete/ajaxSelectSecondary try this and you will see it asks you to log in – V H Jan 23 '15 at 05:28
  • Woah! its working ! I granted the necessary permissions and it just worked. Thats what I love about grails. Thanks for the help. I'm going to work on adding the 4 sub ridings under province next. I'm sure it won't be hard. – Dave Jan 23 '15 at 06:59
  • Hi Vahid, I'm having trouble passing the id from the selectPrimary and selectSecondary to my controller action. I have it working with the regular select dropdown control. Do you have any advise on passing the id from your plugins control to the action? Any thoughts? -Dave – Dave Apr 03 '15 at 17:15
  • Hey Dave. The id will be defined as the collectField. so assuming you have – V H Apr 03 '15 at 17:37
  • Thanks Vihad, I tried to fix the problem and have seemed to have messed up the form aswell. It was working great. Now it should the wrong ridings for each province. I've posted the code if you could look it over. Here is the link. Feel free to login, but the page should show up with login http://54.187.189.129:8080/candidate/showCreateCandidate – Dave Apr 03 '15 at 19:23
  • Hi Dave, unsure if I fully understand your problem What was it set to before as in what was changed for you to notice the difference..I created this gist page https://gist.github.com/vahidhedayati/d6d455d5afc9950728aa which was my selection of a country province which did allow full selection in all boxes. the ids for the first primary secondary are also listed out of the html - – V H Apr 03 '15 at 19:39
  • https://github.com/vahidhedayati/ajaxdependancyselectexample/blob/master/grails-app/views/myContinent/multidomainexample.gsp#L140 here is an example quite similar to yours - the only difference was in your selectSecondary you had bindid and bindid2 I think you only need bindid - but besides that it looked fine to me – V H Apr 03 '15 at 19:59
  • Perfect thanks, Vihad, the form is working now. I'll checkout the example and try it again. Thanks!.| Dave – Dave Apr 03 '15 at 21:35
  • Hi Vihad, one more question about the ajaxdependancyselection. Its working great, but how do I make the selectSecondary not required. I'd like to do the opposite of this: required="required". Please advise. Thanks! Dave – Dave Jun 03 '15 at 03:04
  • I think it's required="false" or try not setting it at all – V H Jun 03 '15 at 06:02