【问题标题】:How to get a list parameter from a grails view如何从 grails 视图中获取列表参数
【发布时间】:2010-10-29 18:16:17
【问题描述】:

在我看来,我有以下几点:

<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

【问题讨论】:

  • 老实说,在这样的视图中创建动态属性(并让它工作),我从来没有成功过。不过,我可能总是做错。

标签: javascript grails gsp


【解决方案1】:

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

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

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

这只是您在亚伦回答的 cmets 中提到的替代解决方案。

【讨论】:

  • 我使用了我在 aarons 答案中发布的替代方案,因为这对于我需要访问的列表来说似乎有点过分了
【解决方案2】:

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

像这样……

[expandableEducationList: ExpandableEducation.list()]

或类似的东西..

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

【讨论】:

  • 实际上,我认为他的做法是相反的——他想知道在表单提交后如何在控制器中获取它。
  • Rob 说的是对的。当我执行 params.each{ println it } 时,我绝对可以看到所有值..将更新它显示的内容
  • 我一定遗漏了一些愚蠢的东西,但似乎 params.expandableEducationList 不应该为空..列表中的 3 个项目中的每一个都肯定有一个字段
  • 看起来如果我使用 params['expandableEducationList[1]'] 我可以获得第一个参数,但这是一种笨拙的方式。猜猜我可以创建一个循环并检查 null 吗?
  • 这会输出什么参数?.expandableEducationList?.each{ println ("it: " + it) }
【解决方案3】:

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

【讨论】:

    猜你喜欢
    • 2013-05-17
    • 2011-06-25
    • 1970-01-01
    • 1970-01-01
    • 2023-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多