0

I want to make chain select box in my view page. I have country select box, and city select box. And in city domain I have country id. Now I want to show cities of a specific country when I select a country. But I have no idea. I am using grails 2.1.0. I have googled on this and tried some codes. But no result. I am giving my domain, controller and view. How can I make the event onchange, create a list of city with the country_id and show it in the city select box? Can anyone please help me on this please ?

my country domain >>>

    package com

class Country {

     String name
    String abbr
    String language

static hasMany = [cities:City]

    static constraints = {
    }
}

my city domain >>>

    package com

class City {

   String name 
   String timezone

static belongsTo = [country:Country]

    static constraints = {
    }
}

my country controller >>>

package com

import com.City
class CountryController {

    def index = { }

}

my view page >>>

<%@ page import="com.Country; com.City" %>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <meta name="layout" content="country" />
</head>
<body>
  <form>
    <g:select
      optionKey="id" optionValue="name" id="countryname" name="countryname"  from="${Country.list()}">
    </g:select>
    <g:select optionKey="id" optionValue="name" id="cityname" name="cityname" from="${City.list()}"></g:select>
  </form>
</body>
</html>
Sumon Bappi
  • 1,937
  • 8
  • 38
  • 82

1 Answers1

1

Try making an Ajax call to fetch the cities for a given country.

See this similar SO question : Populate dropdown list using ajax In grails

Community
  • 1
  • 1
rimero
  • 2,383
  • 1
  • 14
  • 8