0

我是 grails 的新手,我有以下查询。

问题域:

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

  • 子域有一个布尔属性“isValid”。

  • Parent/list.gsp 允许用户通过 ParentController 中的更新函数来更改 ParentInstanceList 中父实例的 isValid 属性,我可以进一步使用它来设置每个父子的属性“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]

}

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

提前致谢

雷曼

4

1 回答 1

0

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

<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 中呈现的内容与您分配属性的内容相同。

于 2010-12-28T15:35:59.583 回答