mjswartz
mjswartz

Reputation: 753

Grails select tag modification

Here's the layout of my problem. I have a Location, Company, and User domain. I am trying to allow an admin to pick a new user's location with a g:select tag. I would like to have it produce this HTML:

<select name="location" id="location">
  <option value="1">Company Name - City 1</option>
  <option value="2">Company Name - City 2</option>
  <option value="3">Company Name - City 3</option>
  <option value="4">Company Name - City 4</option>
  ...
</select>

I can currently do this with

<g:select name="location" 
          from="${locations}" 
          optionKey="id" 
          optionValue="${{it.company.name + ' - ' + it.city}}"/>

and this saves the selection successfully, but when I go to edit the same user, it automatically jumps to a default value of Company Name - City 1 and the admin has to re-select the desired selection. How do I modify my g:select to fix this?

Upvotes: 0

Views: 43

Answers (1)

Joshua Moore
Joshua Moore

Reputation: 24776

You need to set the selected value using the value attribute of the g:select. Something along these lines:

<g:select name="location" 
  value="${user?.location?.id}"
  from="${locations}" 
  optionKey="id" 
  optionValue="${{it.company.name + ' - ' + it.city}}"/>

Upvotes: 1

Related Questions