我有像
User{
static hasMany=[friends:User]
}
我可以为这样的用户获得所有 friend :
user.friends
但是我需要在没有主意的情况下应用分页,有什么建议吗?
最佳答案
您可以使用条件来检索您的User对象。因此,您将能够使用分页:
int maxElementsPerPage = 15
User.createCriteria().list(offset: currentPage * maxElementsPerPage,
max: maxElementsPerPage) {
// condition here
'in' ('id', user.friends*.id)
// I'm not sure if you need to put '*.id'
}
您只需要增加'currentPage'即可为下一页提供元素。
对于Grails标准,您应该查看official Grails criteria documentation
或者,您可以在GSP中使用Grails标记(它将处理所有内容):
<g:paginate controller="user" action="list" total="${userCount}" />
更多详细信息here。
关于grails - 分页与集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37609243/