我在 Controller 中创建了一个 map 并将其传递给 View。我有一个下拉列表,用于填充数据库中的值。值是“1”、“2”、“3”。我想从下拉列表中选择 map 的值(value)
就像第一个下拉列表一样,所选值将是 Map 的第一个元素,对于下一个下拉列表,所选值将是 Map 的第二个值,依此类推,我想这可以从选择选项卡的 value 属性中完成,但是怎么样?任何帮助表示赞赏
例如
我在 Controller 中创建了一个 map 并将其传递给查看
def Population={
def scores=["1":2,"2":3,"3":2,"4":2,"5":3,"6":1]
return new ModelAndView("/scores/scoreCheck",scores)
}
<td><g:select name="SCBFScore" id="SCBFScore" from="${Decode.findAllByAttributeShortName('HASCORE1').collect{it.attributeCodeText}}"
value="${scores?.get('1')}" noSelection="${['':'Select...']}"
</td>
Decode.findAllByAttributeShortName('HASCORE1').collect{it.attributeCodeText}
最佳答案
g:select 的 value 属性有不同的用途。如果要从域对象列表中填充 g:select,则可以指定域类属性,该属性用于存储值 - 或预设,如果它已经包含一些值。
简单的例子。您有一个域菜单和内容,t 并且在菜单中您要分配默认内容(我的代码中的片段,'menuInstance' 是实际的域对象,我正在使用):
<g:select name="defaultContent.id"
from="${my.package.Content.list()}"
optionKey="id"
value="${menuInstance?.defaultContent?.id}" />
return [ 'listbox1': list1, 'listbox2':list2 ]
public class ScoreDTO {
String season
int score
public String toString() { return "Season $season: $score" }
}
ScoreDTO score1 = new ScoreDTO( season:'2012', score:3 )
ScoreDTO score2 = new ScoreDTO( season:'2011', score:1 )
ScoreDTO score3 = new ScoreDTO( season:'2010', score:2 )
def seasons = [ score1, score2, score3 ]
return [ 'seasons':seasons ]
<g:select name="score" from="${ seasons }" />
https://stackoverflow.com/questions/9667430/