在 gsp 中设置 select 值

Set select value in 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 列表)所以我想根据这个设置 select 值:我评论了选项值所以你可以理解我想分配什么值。

查看 gsp:

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

我该怎么做??

我会这样放置你的代码:

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 ]
}

普惠制:

<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>