是否有任何简单的解决方案可以让父显示视图中的子元素的g:pagination和 g:sortableColumn正常工作?
文档中很好地解释了如何为列表中的当前域执行分页和可排序列,并且它可以工作,但我无法让它在所描述的情况下工作。
编辑:我更新了示例
现在唯一不起作用的是分页。当我单击下一页链接时,列表消失并且 ${childrenistSize} 打印 0。
简单的例子:
父域
class Parent {
String name
static hasMany = [children: Children]
static constraints = {
}
}
子域
class Children {
String first
int number
static belongsTo = [parent: Parent]
static constraints = {
}
}
父列表视图
<g:each in="${parentList}" var="parent">
<tr>
<td>${parent.name}</td>
<td><g:link controller="parent" action="show" id="${parent.id}">Show</g:link></td>
</tr>
</g:each>
父显示视图
<table>
<thead>
<tr>
<g:sortableColumn property="first" title="First"/>
<g:sortableColumn property="number" title="Number"/>
<tr>
</thead>
<tbody>
<g:each in="${childrenList}" var="child">
<tr>
<td>${child.first}</td>
<td>${child.number}</td>
</tr>
</g:each>
</tbody>
</table>
<g:paginate total="${childrenListSize}"/>
父控制器
class ParentController {
def index() { }
def list() {
[parentList: Parent.list()]
}
def show() {
params.max=3
def parentInstance = Parent.get(params.id)
def childrens = Children.createCriteria().list(params) {
eq('parent', parentInstance)
}
[parentInstance : parentInstance , childrenList: childrens, childrenListSize: childrens.totalCount]
}
}
一些 BootStrap 测试元素
Parent parent1 = new Parent(name: "TestParent1")
Parent parent2 = new Parent(name: "TestParent2")
Children child1 = new Children(first: "Bob", number: "1")
Children child2 = new Children(first: "John", number: "2")
Children child3 = new Children(first: "Igor", number: "3")
Children child4 = new Children(first: "Lucy", number: "4")
Children child5 = new Children(first: "Lisa", number: "5")
Children child6 = new Children(first: "Bob", number: "12")
Children child7 = new Children(first: "John", number: "24")
Children child8 = new Children(first: "Igor", number: "33")
Children child9 = new Children(first: "Lucy", number: "42")
parent1.addToChildren(child1).addToChildren(child2).addToChildren(child3).addToChildren(child4).addToChildren(child5)
parent2.addToChildren(child6).addToChildren(child7).addToChildren(child8).addToChildren(child9)
parent1.save()
child1.save()
child2.save()
child3.save()
child4.save()
child5.save()
parent2.save()
child6.save()
child7.save()
child8.save()
child9.save()