vahidhedayati/ajaxdependancyselection

retrieve dependent select list from controller action

Closed this issue · 1 comments

By default when a continent is selected (e.g. Europe), all the countries in Europe appear in the secondary (dependant) select. But if (for example) I only want countries in Europe that begin with the letter "I" to appear, is there any way to achieve this?

One way to support this would be to allow a controller and action to be specified, so that when a continent is selected in the primary select, the secondary select is populated with the JSON returned by this action. It would also be necessary to provide the currently selected continent as parameter(s) to this action, e.g.

<g:selectPrimary id="MyContinent2" name="MyContinent2"
    domain="ajaxdependancyselectexample.MyContinent"
    searchField="continentName"
    collectField="id"

    // these attributes are not supported at present
    controller="country"
    action="selectCountries"

    domain2="ajaxdependancyselectexample.MyCountry"
    bindid="mycontinent.id"
    searchField2="countryName"
    appendValue="optional_Additional_Value_"
    appendName="Optional Additional Name"
    collectField2="id"
    noSelection="['': 'Please choose Continent']" 
    setId="MyCountry11"
    value=""/>

Now in order to determine which countries to display in the dependent select, the following action (provided by the application) would be called:

class CountryController {

    def selectCountries() {
        String continentName = params.searchField
        Long continentId = params.collectField

        Continent continent = Continent.get(continentId)
        Map countries = [:]
        Country.findAllByContinentAndNameLike(continent, "I%").each {
            countries[it.id] = it.name
        }
        countries as JSON
    }
}

Notice that the ID and text displayed in the primary select should be passed as parameters to this action. The JSON rendered by this action would look something like:

{
  "2": "Iceland",
  "4": "Ireland"
}

Which the plugin should convert to:

<option value="2">Iceland</option>
<option value="4">Ireland</option>

Hi Sorry

I think this was already possible, I had not been thinking too clearly.

I have updated the example site with the code to do such a thing:

https://github.com/vahidhedayati/ajaxdependancyselectexample/blob/master/grails-app/controllers/ajaxdependancyselectexample/MyContinentController.groovy
line 21 - 43

     def selectCountries() {
         if (params.id) {
              println params
              String continentName = params.searchField
              Long continentId = params.id as Long
              MyContinent continent = MyContinent.get(continentId)
              def countries=MyCountry.findAllByMycontinentAndCountryNameLike(continent, "F%")
              def results = countries.collect {[    'id': it.id, 'name': it.countryName ]}.unique()
              render results as JSON
         }
     }

https://github.com/vahidhedayati/ajaxdependancyselectexample/blob/master/grails-app/views/myContinent/customexample.gsp

example page doing such a thing.

I will be ammending the plugin so that if you do define a custom controller action then there will be no need to provide domain2 and bindid etc since you are going to set up your own logics from that point onwards.

Will close this, let me know if there is still something missing.