如何在控制器中绑定/更新整个实例列表?
我是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我更喜欢为每一行渲染一个专门命名的复选框:
然后收集所有选中的复选框:
您可以使用数组属性,将复选框命名为
parentInstanceList[0]
,然后分配properties = params
到某个具有parentInstanceList
属性的对象,但它很容易出错:您无法保证在 HTML 中呈现的parentInstanceList
与您分配属性的对象。I prefer to render for each line a specifically named checkbox:
and then collect all the checked checkboxes:
You could use array properties, that it, name the checkboxes like
parentInstanceList[0]
, and then assignproperties = params
to some object withparentInstanceList
property, but it's error prone: you have no guarantee that theparentInstanceList
you rendered in HTML will be the same as the that you assign properties to.