简体   繁体   中英

GSP - set integer values for select tag option

In My GSP i have a select tag

<g:select  from="${named}" name="name"  noSelection="['':'-Select-']"  ></g:select>

which produced HTML

<select name="name" id="name">
 <option value="">-Select-</option>
 <option value="USA">USA</option>
 <option value="UK">UK</option>

but i need to have integer value there starting from 1 (i,e USA wil have value 1 and UK 2)

How do i do it

one solution could be that before you return the list to your gsp, convert your list to a map and define the indexes for the countries, so that you have a result like for example..

def named = ["USA","UK"]
def namedMap = [:]
named.eachWithIndex() {obj, i -> namedMap.put(++i, obj) }

which would give a result like

def namedMap = [1:"USA",2:"UK"]

and in your GSP you can do like:

<g:select optionKey="key" optionValue="value" 
    name="name" noSelection="['':'-Select-']" from="${namedMap}" />

Try this..,.

<select name="name">
    <g:each in="${named}" var="country" status="idx">
        <option value="${idx + 1}">${country}</option>
    </g:each>
</select>

您可以在g:select标签中设置选项键 ,如下所示......

<g:select id="name" name="name" from="${name}" optionKey="id" optionValue="${{it.name}}" value="${named}" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM