我是grails的新手,并尝试使用RichUI插件实现treeview,它显示所有父母在Parent.list.gsp中有个别孩子
父母及其子女的xml
<parents name='Parents'>
<Parent id='1' name='Parent_1'>
<Children name='Children'>
<child name='Child_2' id='2' />
<child name='Child_4' id='4' />
<child name='Child_1' id='3' />
<child name='Child_3' id='1' />
</Children>
</Parent>
<Parent id='2' name='Parent_2'>
<Children name='Children'>
<child name='Child_1' id='8' />
<child name='Child_2' id='7' />
<child name='Child_4' id='6' />
<child name='Child_3' id='5' />
</Children>
</Parent>
</parents>
父域类
class Parent {
String name
static hasMany = [children:Child]
}
子域类
class Child {
String name
Parent parent
static belongsTo = [parent:Parent]
}
家长控制器
def list = {
def writer = new StringWriter()
def xml = new MarkupBuilder(writer)
xml.parents(name: "Parents"){
Parent.list().each {
Parent parentt = it
Parent( id:parentt.id,name:parentt.name) {
Children(name:'Children'){
parentt.children.each {
Child childd = it
child(name:childd.name,id:childd.id)
}
}
}
}
}
if(!params.max)params.max=10
["data":writer.toString(),parentInstanceList: Parent.list(params), parentInstanceTotal: Parent.count()]
}
Parent.list.gsp
<head>
<resource:treeView/> ...</head>
<body>
<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="relationship" title="${message(code: 'parent.relationhsip.label', default: 'Relationship')}" />
</tr>
</thead>
<tbody>
<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><richui:treeView xml="${data}" /></td>
</tr>
</g:each>
</tbody>
</table>
</body>
问题
目前,在列表视图中,每个父条目都包含所有父母及其子女的列表 关系栏
父列表视图快照link text
问题
我如何只为每个家长招募所有孩子而不是在每个家长条目中招募所有父母和孩子?
提前致谢
拉赫曼
答案 0 :(得分:0)
这里的问题是你在一个操作中为所有节点渲染XML,然后将它传递给'data',它为每一行显示相同的XML。
您需要做的是将父ID的参数传递给闭包:
/ *这会在你的gsp * /
中的foreach循环上运行def listParentNode = {
def parentId = params.id
def writer = new StringWriter()
def xml = new MarkupBuilder(writer)
xml.parents(name: "Parents"){
Parent.findAllById(parentId).each {
Parent parentt = it
Parent( id:parentt.id,name:parentt.name) {
Children(name:'Children'){
parentt.children.each {
Child childd = it
child(name:childd.name,id:childd.id)
}
}
}
}
}
["data":writer.toString()]
//or render "data" as XML // depending on what the richui tag is expecting.
}
另一个peice在你的gsp中,保留gsp文件完整只需更改此部分:
<td><richui:treeView xml="${createLink(controller:'somecontroller',action:'listParentNode', id: parentInstance.id )}" /></td>
somecontroller是控制器的名称,用于执行这些操作。