How to set selected value when using g:select in grails
Asked Answered
H

3

7

I'm newbie with Grails framwork. I have issue:

I have Domain is: "Country". And I wants to show list country in select tag by using g:select in Grails

For example: The data store in database can see below

ID             Name

1              England
2              Canada
3              Germany

Now, I wants set "Canada" is selected. Who can help me. Please!

Hula answered 18/1, 2013 at 12:42 Comment(0)
A
9

Use the value attribute to set the selected item:

<g:select name="user.company.id"
from="${Company.list()}"
value="${user?.company.id}"
optionKey="id" />
Angie answered 18/1, 2013 at 13:22 Comment(0)
G
3

Why not use g:countrySelect tag? This will save One whole Domain.

Gunslinger answered 3/4, 2014 at 9:14 Comment(2)
This really should be a comment instead of an answer, but I'm voting it up because it's useful and relevant.Guitarist
interesting. I have never see g:XXXSelect before. could you point me to where this is documented? Also need to be able to specify which is the "selected" item.Schnabel
C
1

Use the select tag structure provided by mpccolorado, then to have Canada selected by default change the value to:

value="2"

(the Id for Canada).

Croton answered 18/1, 2013 at 14:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.