-
Notifications
You must be signed in to change notification settings - Fork 5
customControllerActions
Vahid H edited this page Mar 2, 2014
·
2 revisions
By adding your own controller and action, you can take it away and produce your own JSON - you will notice not as much input is then required, using a selectPrimary with no bindings or secondary domaiuns etc
Choose Europe and France will be the only choice since according to:
controller="MyContinent"
action="selectCountries"
It will only list Countries starting with F
<form method=post action=example5>
<label>Continent:</label>
<g:selectPrimary id="MyContinent2" name="MyContinent2"
domain="ajaxdependancyselectexample.MyContinent"
searchField="continentName"
collectField="id"
controller="MyContinent"
action="selectCountries"
noSelection="['': 'Please choose Continent']"
setId="MyCountry121"
value=""/>
<g:select name="MyCountry" id="MyCountry121" from="[]" required="required" noSelection="['': 'Please choose Continent']" />
<input type=submit value=go> </form>
Now within our controller:
class MyContinentController {
....
def selectCountries() {
if (params.id) {
println params
String continentName = params.searchField
Long continentId = params.id as Long
MyContinent continent = MyContinent.get(continentId)
/* Either this method or below method which is much shorter
def primarySelectList = []
MyCountry.findAllByMycontinentAndCountryNameLike(continent, "F%").each {
def primaryMap = [:]
primaryMap.put('id', it.id)
primaryMap.put('name', it.countryName)
primarySelectList.add(primaryMap)
}
render primarySelectList as JSON
*/
// Shorter method
def countries=MyCountry.findAllByMycontinentAndCountryNameLike(continent, "F%")
def results = countries.collect {[ 'id': it.id, 'name': it.countryName ]}.unique()
render results as JSON
}
}