开发者

How to get a list parameter from a grails view

开发者 https://www.devze.com 2023-01-23 05:00 出处:网络
In my view I have the following: <g:hiddenField name=\'expandableEducationList[${edIdx}]._deleted\' value=\'false\'/>

In my view I have the following:

<g:hiddenField name='expandableEducationList[${edIdx}]._deleted' value='false'/>
<button 
                onclick="dojo.byId('expandableEducationList[${edIdx}]._deleted').value='true'; getContainer(this, 'tr').style.display='none'"
                style="width:auto;"
                iconClass="minusIconSmall"
                showlabel="false"
                class="embedded"            
                dojoType="dijit.form.Button"></button>

How do I get that expandableEducationList from my controller? If I do a "println params" I can see the values there, but I want to iterate over the list and params.expandableEducationList is returning null

Here's what I get in my controller when I print the values:

    params?.each{
        println ("it: " + it)
    }

returns

it: expandableEducationList[2].type.id=35
it: expandableEducationList[2]=["lastModified_day":"29", "id":"272", "lastModified_year":"2010", "areaOfStudy":"Computer
 Science", "yearGranted":"2008", "lastModified_month":"9", "_deleted":"false", "_inProcess":"", "type":["id":"35"], "col
legeName":"COLLEGE3", "type.id":"35"]
it: expandableEducationList[1]._deleted=false
it: expandableEducationList[1]=["id":"271", "lastModified_day":"29", "lastModified_year":"2010", "areaOfStudy":"Mathemat
ics and Computer Science", "yearGranted":"2009", "lastModified_month":"9", "_inProcess":"", "_deleted":"false", "type":[
"id":"30"], "type.id":"30", "collegeName":"COLLEGE1"]
it: expandableEducationList[2].id=272
it: _action_update=
it: expandableEducationList[1].lastModified_month=9
it: expandableEduca开发者_JS百科tionList[2].yearGranted=2008
it: expandableEducationList[1].collegeName=COLLEGE1
it: id=518
it: _expandableEducationList[2].inProcess=
it: expandableEducationListx[0]_name=42
it: expandableEducationList[0].areaOfStudy=Systems Engineering
it: expandableEducationList[0]=["lastModified_day":"29", "id":"270", "lastModified_year":"2010", "yearGranted":"2010", "
areaOfStudy":"Systems Engineering", "lastModified_month":"9", "_deleted":"false", "_inProcess":"", "type":["id":"42"], "
type.id":"42", "collegeName":"COLLEGE2"]
it: expandableEducationList[2].lastModified_day=29
it: expandableEducationList[0].id=270
it: expandableEducationList[2]._deleted=false
it: expandableEducationList[0].lastModified_day=29
it: geographicPreferences=
it: expandableEducationList[2].areaOfStudy=Computer Science
it: expandableEducationList[2].collegeName=COLLEGE3
it: expandableEducationListx[1]_name=30
it: expandableEducationList[1].lastModified_day=29
it: _expandableEducationList[0].inProcess=
it: _expandableEducationList[1].inProcess=
it: expandableEducationList[0]._deleted=false
it: expandableEducationList[1].yearGranted=2009
it: expandableEducationList[1].lastModified_year=2010
it: expandableEducationList[2].lastModified_month=9
it: expandableEducationList[1].areaOfStudy=Mathematics and Computer Science
it: expandableEducationList[2].lastModified_year=2010
it: expandableEducationList[1].id=271
it: expandableEducationListx[2]_name=35
it: expandableEducationList[0].yearGranted=2010
it: expandableEducationList[0].lastModified_month=9
it: expandableEducationList[0].collegeName=COLLEGE2
it: expandableEducationList[0].lastModified_year=2010
it: expandableEducationList[0].type.id=42
it: expandableEducationList[1].type.id=30


The "Grailsy" way to do this is to use Command objects and Data Binding. Look at Data Binding and Many-ended Associations here.

With that, you could create a Command object that maps to your association and then bind the request parameters using something like:

def command = new MyCommand()
bindData(command, params)

This is just an alternative solution to the one you mentioned in the comments on Aaron's answer.


pass it in as part of the model in your controller action that is rendering the gsp

like this...

[expandableEducationList: ExpandableEducation.list()]

or something like this..

  render(view: 'viewName', 
         model: [expandableEducationList: ExpandableEducation.list()])


By assigning the names like this: name='expandableEducationList[${edIdx}]._deleted'

The params object is populated with a parameter literally named "expandableEducationList[1]._deleted" and so on, versus a list named "expandableEducationList" which you could access by index.

Other StackOverflow threads with the same topic:

  • Grails indexed parameters
  • Grails Indexed Properties with No Real Index

Check out the linked threads first as they may work best.

Here is another possible solution if you know the count of items and names of properties:

//count is the number of items 
def expendableEducationList=(0..count).collect{
    index->
    // propNames is a list of names such as ['_.deleted', 'areaOfStudy', etc..] 
    // returns a new map with each property value
    propNames.inject([:],{
        map, prop->
        map[prop]=params["expandableEducationList[$index].$prop"]
        return map
    } 
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号