在gsp中设置选择值

时间:2015-08-19 12:18:35

标签: grails gsp

控制器:

     def abbrev = [:]
        abbrev.put(1,"I")
        abbrev.put(2,"II")
        abbrev.put(3,"III")
        abbrev.put(4,"IV")
        List<Expando> abbreviations = abbrev.collect{
          abbreviation -> Expando.newInstance(key:abbreviation.key,value:abbreviation.value)
        }

            def row = [:]
            def programRows = [ ]

    somelist.each {
              item -> row = [key1:value1, key2,value2 ]
                programRows << row
            }

[abbreviations:abbreviations, programRows:programRows ]

我遍历programRows,所以我得到一张地图(programRow)。  map value1相当于缩写中的一个键(Expandos列表)所以我想根据这个设置选择值:我评论了选项值,这样你就可以理解我想要分配的值。

查看gsp:

    <g:each in="${programRows}" var="programRow">
   <g:select name="abbrevs" from="${abbreviations}" optionValue="//programRow.get('key1')//" optionKey="key" class="vfuCourseAbbreviations"/>

我怎么能这样做?

1 个答案:

答案 0 :(得分:1)

我将你的代码放在:

def someAction(){
  def abbreviations = [ 'I', 'II', 'III', 'IV' ]
  def programRows = somelist.collect {
    [ key1:value1, key2:value2 ] // as "value1" you have to pass the index in "abbreviations"
  }
  [ abbreviations:abbreviations, programRows:programRows ]
}

GSP:

<g:each in="${programRows}" var="programRow">     
  <select name="abbrevs">
    <g:each in="${abbreviations}" var="abbr" status="i">
      <option value="${i}" ${i == programRow.key1 ? 'selected' : ''}>${abbr}</option>
    </g:each>
  </select>     
</g:each>