简体   繁体   English

在gsp中设置选择值

[英]Set select value in gsp

Controller: 控制器:

     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 ]

I iterate through programRows so i get a map ( programRow ). 我通过programRows进行迭代,所以我得到了一张地图(programRow)。 the map value1 is equivalent to a key in abbreviations ( List of Expandos ) so i want to set the select value based on this: i commented the option value so you can understand what value i want to assign. 映射value1等效于缩写键(Expandos列表),因此我想基于此设置选择值:我评论了选项值,以便您可以了解要分配的值。

View gsp: 查看gsp:

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

How can i do this?? 我怎样才能做到这一点??

I'd put your code so: 我将您的代码放入:

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

the GSP: 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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