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

发布于 2024-09-29 18:53:04 字数 3272 浏览 3 评论 0原文

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

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

如何从我的控制器获取可扩展教育列表?如果我执行“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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

花开浅夏 2024-10-06 18:53:04

执行此操作的“Grailsy”方法是使用命令对象和数据绑定。查看数据绑定和多端关联 此处

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

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

这只是您在亚伦答案的评论中提到的解决方案的一种替代解决方案。

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.

话少情深 2024-10-06 18:53:04

将其作为模型的一部分传递到控制器操作中,该操作正在渲染 gsp

,如下所示......

[expandableEducationList: ExpandableEducation.list()]

或类似的东西......

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

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()])
满栀 2024-10-06 18:53:04

通过分配这样的名称:
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
    } 
}

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:

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
    } 
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文