我有一个查询,它给出了大约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 ])
}
它给了我大约100行的结果集,但它们出现在同一页面内。 现在我想使用分页,这样我就可以将它限制为每页只有20行。 我应该怎么做,以及如何使用分页。
答案 0 :(得分:7)
在您的分页标签上,检查总参数。那应该是记录的总数。在您的情况下为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]
}
答案 1 :(得分:0)
如果我没记错的话,你将max和offset属性添加到你传递给视图的模型中,它应该自动加入分页。此时,您应该能够使用paginate标记迭代结果集。请参阅此处的文档:
答案 2 :(得分:0)
Grails Criteria查询和分页参数
params.max = params?.max as Integer ?: 10
params.offset = params?.offset as Integer ?: 0
params.sort = params?.sort ?: "email"
params.order = params?.order ?: "asc"
params.filter = params?.filter ?: ""
params.packet = params?.packet ?: ""
def members = Member.createCriteria().list(params)
{
or
{
if(params.filter != ""){
ilike("firstName", "%" + params.filter + "%")
ilike("lastName", "%" + params.filter + "%")
ilike("email", "%" + params.filter + "%")
try {
params.filter as Long
eq("citizenId" , params.filter.toLong())
}catch (e) {
}
ilike("mobile", "%" + params.filter + "%")
}
}
}
def dataMembers = [:]
dataMembers.data = members
dataMembers.totalRecord = members.totalCount
render dataMembers as JSON
<强>输出强>
{
"data": [
{
"id":1,
"firstName":name
},
{
"id":2,
"firstName":name
}
],
"totalRecord":5
}