简体   繁体   中英

Tag “Select” in gsp

How to correctly use tag select in gsp while passing to it ArrayList<String> ? I know how to use it this my own composed objects, for example:

<g:select name="tablePlacesAvailable" from="${tableInfo}" optionValue="${{it.placesInTableAmount}}" optionKey="placesInTableAmount" value=""/>

But how to use it with built in objects, like String ?

If you want to use a g:select with a list of String

<g:select name="selection" from="${values}" />

Where values is a collection of Strings. My controller code is

class DemoController {
    def index() { 
        [values: ["This", "is", "a", "test"]]
    }
}

Here are some tests and its results: Controller: Controller:

 def index() {
        def myList=['hello','world']
        def myMap=['h':'hello','w':'world']
        render view: 'index', model: [myList:myList,myMap:myMap]
    }

gsp:

<g:form name="test" >
 <g:select name="s1" from="${['lastUpdated', 'id']}" value="${sortby}"  />
 <g:select  name="s2" from="${['lastUpdated', 'id']}"
  optionKey="value" optionValue="value"  value="${sortby}"  />

 <g:select name="g"  from="${['90%':'90%','100%': '100%']}" 
 optionKey="key" optionValue="value" value="${params.g}" />

 <g:select name="g2"  from="${myList}" />

 <g:select name="g3"  from="${myMap}" />

 <g:select name="g4"  from="${myMap}" optionKey="key" optionValue="value"/>
 <g:select name="g5"  from="${myMap}" optionKey="key" optionValue="key"/>
 <g:select name="g6"  from="${myMap}" optionKey="value" optionValue="value"/>

<g:submitButton name="go" value="post"/>
</g:form>

Results:

[g:100%, s2:lastUpdated, s1:id, g2:world, g6:world, g5:w, g4:w, 
go:post, g3:w=world, action:index, format:null, controller:test] 

As you can see a Arraylist with no optionKey optionValue works by default, in s2 I have also physically defined key value to be value and it still works

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