Grails Cookbook - A collection of tutorials and examples

Grails countrySelect Tag Example

The Grails countrySelect tag (<g:countrySelect>) can be used to generate a dropdown list of countries. This will show a simple example on how to use this.

Introduction

Instead of writing an HTML code like this

<select name="countryCode" id="countryCode" >
<option value="">-Choose your country-</option>
<option value="afg" >Afghanistan</option>
<option value="alb" >Albania</option>
<option value="dza" >Algeria</option>
...
<option value="esh" >Western Sahara</option>
<option value="yem" >Yemen</option>
<option value="zmb" >Zambia</option>
</select>

Where you need to prepare the list of countries, either in GSP or on a table, you can just do this equivalent:

<g:countrySelect name="countryCode" 
    value="${shopInstance?.countryCode}"
    noSelection="['':'-Choose your country-']"/>

The generated code is the same as the HTML above. The value is the ISO3166_3 country code (E.g. afg, alb, dza), and the description is the full name of the country (E.g. Afghanistan, Albania, Algeria).

Example

Here is a sample application screenshot on how the countrySelect tag is used:
zz1

zz2
Here is the domain class used:
package asia.grails.demo
class Shop {
    String name
    String streetAddress
    String countryCode
    static constraints = {
    }
}
We just use a string column to receive the country code value.

And this is a sample GSP code:

<g:countrySelect name="countryCode"
    value="${shopInstance?.countryCode}"
    noSelection="['':'-Choose your country-']"/>

And this will automatically generate the HTML code for all available countries from Afghanistan to Zimbabwe as shown in the HTML above in the introduction.
Note: the value saved in the column will be the ISO code and not the country name as shown in the listing above.

Specific Country

It is possible to display selection on selected countries only:

<g:countrySelect name="countryCode"
    value="${shopInstance?.countryCode}"
    from="['ita', 'fra', 'deu']"
    noSelection="['':'-Choose your country-']"/>

Will render this HTML:

<select name="countryCode" id="countryCode" >
<option value="">-Choose your country-</option>
<option value="ita" >Italy</option>
<option value="fra" >France</option>
<option value="deu" >Germany</option>
</select>

Customize Country Names

The rendered country names can be customized. Choose any prefix (E.g. countryname) and put entries in messages.properties

countryname.ita=Spaghetti
countryname.fra=Wine
countryname.deu=Beer

And this code:

<g:countrySelect name="countryCode"
    value="${shopInstance?.countryCode}"
    from="['ita', 'fra', 'deu']"
    noSelection="['':'-Choose your country-']"
    valueMessagePrefix="countryname" />

Will render:
<select name="countryCode" id="countryCode" >
<option value="">-Choose your country-</option>
<option value="ita" >Spaghetti</option>
<option value="fra" >Wine</option>
<option value="deu" >Beer</option>
</select>

Other Grails Tag Examples


Tags: countrySelect, Grails Tag