Set Option key and value in Grails?

I am currently working on a Grails application, and I am browsing the selection list on the page and setting its value to one value and the value to another. The code for all related items is below:

Domain Object

class Cars { String carType String car } 

Controller:

 def create() { List cars = Cars.list() [dealerInstance: new Dealer(params), cars: cars] render(text:"This site in curently under Maintainence!") } 

View:

 <g:select name="cars.id" from="${cars}" optionKey="${carType}" optionValue="${car}"/> 

Now, when I run this code, the list is populated, however the data looks like this:

 <select id="cars.id" name="cars.id"> <option value="com.app.Cars : 1">com.app.Cars : 1</option> <option value="com.app.Cars : 2">com.app.Cars : 2</option> <option value="com.app.Cars : 3">com.app.Cars : 3</option> </select> 

and then when I try this:

View:

 <g:select name="cars.id" from="${cars.car}" optionKey="${carType}" optionValue="${car}"/> 

I get the following:

 <select id="cars.id" name="cars.id"> <option value="Audi">Audi</option> <option value="BMW">BMW</option> <option value="Scoda">Scoda</option> </select> 

This is not at all what I want. I want to be able to set another pair of key values ​​in this selection list, so that I can use this data in my application, I would like the following to be created:

 <select id="cars.id" name="cars.id"> <option value="Saloon">Audi</option> <option value="Hatch Back">BMW</option> <option value="Estate">Scoda</option> </select> 

Can someone please tell me how I achieve this in Grails ???

+4
source share
1 answer

Try the following:

 <g:select id="cars.id" name="cars.id" from="${cars}" optionKey="carType" optionValue="car"/> 

What basically happens:

When you use the from attribute, the tag iterates through the list that you passed as an argument. optionKey and optionValue should be strings representing the attributes that you want to access in the current object (which the iterator refers to).

+8
source

Source: https://habr.com/ru/post/1444389/


All Articles