【问题标题】:how to bind/update Whole InstanceList in Controller?如何在控制器中绑定/更新整个 InstanceList?
【发布时间】:2010-12-28 14:56:26
【问题描述】:

我是 grails 的新手,我有以下疑问。

问题域:

  • 父域类有一个布尔值 瞬态属性“isValid”。

  • 子域具有布尔属性 “有效”。

  • Parent/list.gsp 允许用户更改 父实例的 isValid 属性 在 ParentInstanceList 中,通过更新 ParentController 中的函数,我 可以进一步使用来设置每个父母 子属性“isValid”。

父域类

class Parent {

String name
boolean isValid = false

static hasMany = [children:Child]
static transients =['isValid']

}

子域类

class Child {

String name
boolean isValid
Parent parent

static belongsTo = [parent:Parent]

}

parent/list.gsp

    <g:form method="post" action="update" >
    <div class="list">
        <table>
            <thead>
                <tr>

                    <g:sortableColumn property="id" title="${message(code: 'parent.id.label', default: 'Id')}" />

                    <g:sortableColumn property="name" title="${message(code: 'parent.name.label', default: 'Name')}" />
                     <g:sortableColumn property="isValid" title="${message(code: 'parent.isvalid.label', default: 'isValid?')}" />

                </tr>
            </thead>
            <tbody>
            <g:hiddenField name="parentInstanceList" value="${parentInstanceList }"/>
            <g:each in="${parentInstanceList}" status="i" var="parentInstance">

                <tr class="${(i % 2) == 0 ? 'odd' : 'even'}">

                    <td><g:link action="show" id="${parentInstance.id}">${fieldValue(bean: parentInstance, field: "id")}</g:link></td>

                    <td>${fieldValue(bean: parentInstance, field: "name")}</td>
                    <td>
                    <g:checkBox name="isValid" value="${parentInstance?.isValid }"/>
                    </td>

                </tr>
            </g:each>
            </tbody>
        </table>
    </div>
    <div class="paginateButtons">
        <g:paginate total="${parentInstanceTotal}" />
    </div>

    <div class="buttons">
        <span class="button">
            <g:actionSubmit class="update" action="update" value="${message(code: 'default.button.update.label', default: 'update')}" />
        </span> 
    </div>

父/list.gsp 快照:http://img156.imageshack.us/i/parentlistview.png/

查询: 我如何追踪哪个 parentInstance 的属性“isValid”未被选中/未选中,我可以轻松设置父级的子属性“isvalid”。

def parentInstanceList  = params.parentInstanceList
    parentInstanceList.each {
        Parent parent = it
        parent.children.each{
            Child child = it
            child.isValid=parent.isValid
        }
    }

到目前为止,我能够通过 params.parentInstanceList 检索所有父母的 id,但我无法弄清楚如何绑定每个 parentInstance 的更改属性。 如果只有一个 parentInstance,那么可以很容易地做到这一点

Parent parentInstance = Parent.get(params.id)
 parentInstance.properties = params

提前致谢

雷曼

【问题讨论】:

    标签: grails grails-controller


    【解决方案1】:

    我更喜欢为每一行渲染一个特别命名的复选框:

    <g:checkBox
            name="Parent_${parentInstance.id}"
            value="${parentInstance.isValid ? 'on' : 'off'}" />
    

    然后收集所有选中的复选框:

    def update = {
        Collection<Parent> checked = params.collect{ Map.Entry that ->
            that.value != "on" ? null : getParentForCheckbox(that.key)
        }.findAll{ it != null }
    
        ... // and then assign 'isValid' by hand
    }
    

    您可以使用数组属性,将复选框命名为parentInstanceList[0],然后将properties = params 分配给具有parentInstanceList 属性的某个对象,但这很容易出错:您无法保证parentInstanceList 您在 HTML 中呈现的将与您分配属性的相同。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-05-19
      • 2014-07-26
      • 2014-08-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-14
      相关资源
      最近更新 更多