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

Javascript 如何从grails视图中获取列表参数,javascript,grails,gsp,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'

我认为我有以下几点:

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

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

在呈现gsp的控制器操作中将其作为模型的一部分传入

像这样

[expandableEducationList: ExpandableEducation.list()]
或者像这样的

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

实现这一点的最佳方法是使用命令对象和数据绑定。看看数据绑定和许多结束的关联

这样,您就可以创建一个映射到关联的对象,然后使用以下方法绑定请求参数:

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

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

通过如下方式指定名称: name='expandableEducationList[${edIdx}].\u已删除'

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

事实上,我认为他是在走另一条路——他想知道在提交表单后,他如何在控制器中获得它。罗伯说的是正确的。当我使用params.each{println it}时,我完全可以看到所有的值..将更新它显示的内容我一定缺少一些愚蠢的东西,但看起来params.expandableEducationList不应该为null..列表中的3个项目都有一个明确的字段,如果我使用params['expandableEducationList[1]”,看起来确实如此我可以得到第一个的参数,但这是一种笨拙的方法。我想我可以做一个循环并检查null?这个转储参数是什么?.expandableEducationList?.each{println it:+it}我真的从来没有在这样的视图中成功创建动态属性并使其工作过。不过,我可能总是做错事。我使用了我在aarons Response中发布的备选方案,因为对于我需要进入的列表来说,这似乎有些过分
//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
    } 
}