Grails 分页 : how to use pagination to restrict the number of rows

标签 grails

我有一个查询,其结果约为 100 行。这是 Controller 的代码

def bandsOneTrack = {
    def bands = Band.executeQuery("Select b from Band as b where size(b.tracks) > (select count(t.id) from Band as ba join ba.tracks as t where ba.id = b.id and t.ourContract is not null) and (select count(t.id) from Band as ba join ba.tracks as t where ba.id = b.id and t.ourContract is not null) >= 1" )
    render(view: 'bands_list' , model: [ bands : bands ])
}

它为我提供了大约 100 行的结果集,但它们出现在同一页面内。 现在我想使用分页,以便将其限制为每页仅 20 行。 我应该做什么,以及如何使用分页。

最佳答案

在分页标签上检查总参数。这应该是记录总数。在您的情况下为 100,以便分页标记可以计算总页数。

类似这样的here :

<g:paginate controller="Book" action="list" total="${bookInstanceTotal}" />

您可能需要执行一次查询才能找到记录总数。

def list() {
    params.max = Math.min(params.max ? params.int('max') : 10, 100)
    def ls = Book.executeQuery("from Book a",[max: params.max, offset: params.offset])
    def totalCount = Book.executeQuery("from Book a").size()
    [bookInstanceList: ls, bookInstanceTotal: totalCount]
}

关于Grails 分页 : how to use pagination to restrict the number of rows,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9518306/

相关文章:

mongodb - 从 Grails 应用程序更新 Mongo 文档字段

java - UnitTest (groovy + grails) - 无法拆卸元类

grails - 使Grails Spring Security通过电子邮件对用户进行身份验证

hibernate - Grails withNewSession 不会刷新

grails - Controller 返回 404 错误,即使它返回结果

database - 排序前的Grails映射值

eclipse - 无法在 GGTS 上切换到 Groovy 2.4

grails - Grails调用服务错误

grails - 基于BuildConfig.groovy中的Environment的WAR名称

json - 如何从 JSON 外部文件加载或引导数据到 grails 中?