Grails:对关系中的集合进行分页和排序

发布于 2024-11-26 04:59:30 字数 988 浏览 1 评论 0原文

我想从关系中的集合进行分页和排序

例如使用以下模型:

class User {
   String userName, password
   static hasMany = [roles: UserRole, preferences: Preference]
}

class UserRole {
   String name, description

   static hasMany = [actions: Action]
}

我想恢复特定用户的所有角色。我已经加载了用户,所以正常的方法是使用

user.roles

但我想按 UserRole 属性对它们进行排序,并且我想动态地对它们进行分页

,我知道如果我想获取所有 <我可以使用 code>UserRole 进行排序和分页:

UserRole.list([sort: 'name', order: 'asc',max: 5,offset:0])

但我只想为与我的用户关联的角色执行此操作。我试图使用标准,但我认为我遗漏了一些东西。

我也看了一下这里: http://grails.1312388.n4.nabble.com /A-Relationship-Paging-Pattern-td1326643.html

但是我必须添加关系回到 UserRole 所以我会:

static hasMany = [users : UserRole]

我该怎么做?最好的方法是什么?

如果您需要更多信息,请告诉我,如果我不够清楚,请告诉我

谢谢和问候

I'd like to do paging and sorting from a collection in a relationship

For example with the following model:

class User {
   String userName, password
   static hasMany = [roles: UserRole, preferences: Preference]
}

class UserRole {
   String name, description

   static hasMany = [actions: Action]
}

I'd like to recover all the roles for a specific user. I already have the user loaded so the normal way to do it would be using

user.roles

But I want to sort them by UserRole properties and I want to paginate them dynamically

I know that if I want to get all the UserRoles sorted and paginated I can use:

UserRole.list([sort: 'name', order: 'asc',max: 5,offset:0])

But I want to do it just for the roles that are associated to my user. I was trying to use criteria, but I think I'm missing something.

I also had a look here:
http://grails.1312388.n4.nabble.com/A-Relationship-Paging-Pattern-td1326643.html

But then I would have to add the relation back into UserRole so I would have:

static hasMany = [users : UserRole]

How can I do this? what would be the best way?

Please, let me know if you need more information and sorry if I wasn't clear enough

Thanks and regards

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

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

发布评论

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

评论(2

不如归去 2024-12-03 04:59:30

您无法对“普通”关系进行分页。

您可以使用映射 DSL 更改子对象的显示顺序:

static mapping = {
    sort name:desc
}

要简化手工制作的分页关系,您可以使用命名查询:

class Role {
    static namedQueries = {
        userRoles {
            eq('user', UserSessionService.instance.currentUser)
        }
    }
}

或者您可以实现瞬态 User 的属性,该属性将为 User 返回一个 Criteria角色(可以分页)。

You cannot paginate an "ordinary" relationship.

You can change the order child objects appear in using mapping DSL:

static mapping = {
    sort name:desc
}

To simplify a hand-crafted paginated relationship, you can use a named query:

class Role {
    static namedQueries = {
        userRoles {
            eq('user', UserSessionService.instance.currentUser)
        }
    }
}

Or you can implement a transient User's property that will return a Criteria for User's Roles (which can be paginated).

孤独岁月 2024-12-03 04:59:30

Grails 分页与 hasmany 关系双向属性终于我说到了重点,我发现它工作了Huuuh。

这些是域类

class Client {
    List bills
    String shopName
    String nameOfClient
    static hasMany = [bills: Bill]

    static constraints = {
        shopName(blank:true, nullable:true)
        nameOfClient(blank:false, nullable:false)       

    }

}

class Bill {
    String billDetails
    String billNo    
    static belongsTo = [client: Client]

    static constraints = {
        billDetails(blank:true, nullable:true , type: 'text')       
        billNo(blank:true, nullable:true)

    }

}

现在这是我的控制器逻辑

 def clientDetails(){
    def maxJobs = 4 
    def offset = (params?.offset) ?: 0
    def clientId = params.id
    def bills = Client.get(clientId).bills
    def client= Client.get(clientId)
    def results = Bill.withCriteria {
        eq('client', client)
        firstResult(offset as Integer)
        maxResults(maxJobs)
    }

    [id:client.id,bills: results, offset: offset, max: maxJobs, totalJobs:   bills.size()]
}

和 gsp 代码

 <g:each in="${bills}">
       <tr>
           <td>${it.billNo}</td>
           <td>${it.billDetails}</td>          
       </tr>
 </g:each>



<g:paginate class="pagination" controller="client" action="clientDetails" total="${totalJobs?:0}" offset="${offset}" max="${max}" params="[id:"${id}"]"
                prev="« Previous" next="Next »" />

Grails Pagination with hasmany relation Bidirectional property finally i come to the point were i found it working Huuuh.

These are the Domain classes

class Client {
    List bills
    String shopName
    String nameOfClient
    static hasMany = [bills: Bill]

    static constraints = {
        shopName(blank:true, nullable:true)
        nameOfClient(blank:false, nullable:false)       

    }

}

class Bill {
    String billDetails
    String billNo    
    static belongsTo = [client: Client]

    static constraints = {
        billDetails(blank:true, nullable:true , type: 'text')       
        billNo(blank:true, nullable:true)

    }

}

Now This is my controller Logic

 def clientDetails(){
    def maxJobs = 4 
    def offset = (params?.offset) ?: 0
    def clientId = params.id
    def bills = Client.get(clientId).bills
    def client= Client.get(clientId)
    def results = Bill.withCriteria {
        eq('client', client)
        firstResult(offset as Integer)
        maxResults(maxJobs)
    }

    [id:client.id,bills: results, offset: offset, max: maxJobs, totalJobs:   bills.size()]
}

And the gsp code

 <g:each in="${bills}">
       <tr>
           <td>${it.billNo}</td>
           <td>${it.billDetails}</td>          
       </tr>
 </g:each>



<g:paginate class="pagination" controller="client" action="clientDetails" total="${totalJobs?:0}" offset="${offset}" max="${max}" params="[id:"${id}"]"
                prev="« Previous" next="Next »" />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文