Grails Pagination:如何使用分页来限制行数

viv*_*vek 2 grails

我有一个查询,它给出了大约100行的结果.这是控制器的代码

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 ])
}
Run Code Online (Sandbox Code Playgroud)

它给了我大约100行的结果集,但它们出现在同一页面内.现在我想使用分页,这样我就可以将它限制为每页只有20行.我该怎么做,以及如何使用分页.

Ali*_*dad 7

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

这里有类似的东西:

<g:paginate controller="Book" action="list" total="${bookInstanceTotal}" />
Run Code Online (Sandbox Code Playgroud)

您可能需要执行一次查询以查找记录总数.

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]
}
Run Code Online (Sandbox Code Playgroud)