我有一个查询,其结果约为 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/