如何在控制器中绑定/更新整个实例列表?

发布于 2024-10-09 18:28:55 字数 3237 浏览 3 评论 0原文

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

问题域:

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

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

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

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

提前感谢

Rehman

I am new bie in grails and i have following query.

Problem Domain:

  • Parent Domain Class has a boolean
    transient property "isValid".

  • Child Domain has a boolean property
    "isValid".

  • Parent/list.gsp allow users to alter
    isValid property of Parent Instance
    in ParentInstanceList, over an update
    function in ParentController, which i
    can further use to set each Parents
    children's property "isValid".

Parent Domain Class

class Parent {

String name
boolean isValid = false

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

}

Child Domain Class

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>

Parent/list.gsp snapshot : http://img156.imageshack.us/i/parentlistview.png/

Query:
how can i track down which parentInstance's property "isValid" is not checked/unchecked that i can easily set parent's children property "isvalid" .

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

So far i am able to retrieve ids of all parents over params.parentInstanceList but i can not figure it out how can i bind changed properties of each parentInstance.
if there is a single parentInstance then one can easily do it

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

thanks in advance

Rehman

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

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

发布评论

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

评论(1

南烟 2024-10-16 18:28:55

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

<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 属性的对象,但它很容易出错:您无法保证在 HTML 中呈现的 parentInstanceList 与您分配属性的对象。

I prefer to render for each line a specifically named checkbox:

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

and then collect all the checked checkboxes:

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
}

You could use array properties, that it, name the checkboxes like parentInstanceList[0], and then assign properties = params to some object with parentInstanceList property, but it's error prone: you have no guarantee that the parentInstanceList you rendered in HTML will be the same as the that you assign properties to.

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