是否有任何解决方案可以获得g:pagination和g:sortableColumn for parents元素在父母显示视图中工作?

时间:2012-12-05 19:19:43

标签: grails gsp

是否有任何简单的解决方案可以获取父节目视图中的子元素的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}"/>

ParentController

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()

2 个答案:

答案 0 :(得分:0)

问题是您在Children域类中没有名为children.name的列。您需要做的是在控制器中识别您必须按名称列排序。

我可以看到您在Parent的show操作中显示了一个子列表,并且父操作在此操作中不可排序,因此将进行简单的更改:

def show() {
  def parentInstance = Parent.get(params.id)
  def childrens = Children.createCriteria().list(params) {
    eq('parent', parentInstance)
  }
  [parentInstance : parentInstance , childrenList: childrens, childrenListSize: childrens.totalCount]
}


<p>Parent: ${parentInstance.name}</p>
<table>
    <thead>
        <tr>
            <g:sortableColumn property="name" title="Name"/>
        <tr>
    </thead>
    <tbody>
        <g:each in="${childrenList}" var="child">
            <tr>
                <td>${child.name}</td>
            </tr>
        </g:each>
    </tbody>
</table>
<g:paginate max="1" total="${childrenListSize}"/>

请注意,我更改了property name,因为它只是名称而不是children.name。此外,我将params传递给标准以进行排序。

我没有对此进行测试,因此在任何地方都可能出现错误。

答案 1 :(得分:0)

好的,我已经回答了我的问题:

.createCryteria只解决了列排序问题,所以你需要在父控制器的show动作中添加这行代码:

def totalChildrenListSize = Children.findAllByParents(parentInstance, [params])

获取特定父级所有子级的列表,因此可以通过调用.size()([....,totalChildrenListSize:totalChildrenListSize.size()]方法获取大小在它上面。

按照Sergio的建议调用孩子们的totalCount,然后为该show动作设置param.max并更改添加id参数的分页标记:

<g:paginate id="${parentInstance.id}" total="${totalChildrenListSize}"/>
相关问题