如何从grails视图中获取列表参数

时间:2010-10-29 18:16:18

标签: javascript grails gsp

在我看来,我有以下内容:

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

如何从我的控制器获取expandableEducationList?如果我执行“println params”,我可以在那里看到值,但是我想迭代列表并且params.expandableEducationList返回null

这是打印值时控制器中的内容:

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

返回

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: expandableEducationList[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

3 个答案:

答案 0 :(得分:0)

将其作为模型的一部分传递给渲染gsp的控制器操作

像这样......

[expandableEducationList: ExpandableEducation.list()]

或类似的东西..

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

答案 1 :(得分:0)

“Grailsy”方法是使用Command对象和数据绑定。查看数据绑定和多端关联 here

有了这个,您可以创建一个映射到您的关联的Command对象,然后使用以下内容绑定请求参数:

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

这只是你在Aaron回答的评论中提到的另一种解决方案。

答案 2 :(得分:0)

通过分配如下名称: name='expandableEducationList[${edIdx}]._deleted'

params对象填充了一个名为“expandableEducationList [1] ._ deleted”的参数,依此类推,而不是名为“expandableEducationList”的列表,您可以通过索引访问该列表。

具有相同主题的其他StackOverflow线程:

首先检查链接的线程,因为它们可能效果最好。

如果你知道物品的数量和物业的名称,这是另一种可能的解决方案:

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