Grails indexed parameters

前端 未结 4 1199
悲&欢浪女
悲&欢浪女 2020-12-28 10:10

I have a list of Team objects that have an Integer seed property. I want to edit all the teams\' seeds at once, in a single form. I\'m sure

4条回答
  •  有刺的猬
    2020-12-28 11:08

    In 2015.... Grails works a little differently now and you may find yourself running into strings rather than the expected sub-maps.I got something to work by doing
    something like..

    params.nested.each{
                   if(!it.getKey().contains('.')){
                     //to get a map rather than a string...    
                      params.nested[it.getKey()];
                   }
               };
    

    EDIT: By The Way...

    inputs that have the same name, like

      
      < input name="item.choice" type="checkbox" value="4"/>
    

    Are put into a List IF more then one are submitted. So if both of the above were checked

       
       < input name="item.choice" type="checkbox" value="4" checked/>
    

    You would get a list.

    But if only one is checked you do NOT get a List(at least in grails verison I use), you get a single value.

         
         < input name="item.choice" type="checkbox" value="4" />
    

    That means in a controller, if I were to do something like

      params['item.choice'].each{
              def item=Item.get(it)
      }
    

    It would throw an error if only one item was submitted. One groovy way to get around this is

     ([]+(params['item.choice']?:[])).each{
             def item=Item.get(it)
     } 
    

    If the parameter is set and not a list, it puts the value into the empty list; If the parameter is set and a list, the plus operator will add all the individual values to the empty list; if the parameter is not set, it will add two empty lists together, which creates a single empty list.

提交回复
热议问题