paginate
用途
创建下一个/上一个 按钮和一个 breadcrumb 实现分页.
示例
domain class实例:
class Book {
String title
String author
}
controller实例:
class BookController {
def list = {
[books: Book.list(params)]
}
}
标签页代码:
<g:paginate controller="book" action="list" total="${Book.count()}" />
或
<g:paginate next="Forward" prev="Back"
maxsteps="0" controller="book"
action="list" total="${Book.count()}" />
描述
属性
total
(必需) - 页数的总数量.
action
(可选) - 要链接的action名称,如果不指定,链接到当前action.
controller
(可选) - 要链接的controller
名称,如果不指定,链接到当前的controller
.
id
(可选) - link的id
params
(可选) - 包含请求参数的 map.
prev
(可选) - 显示上一页 (根据默认值定义 "Previous" 做为默认的分页)
next
(可选) - 显示下一页
max
(可选) - 每页显示的总记录 (默认为 10条记录).
maxsteps
(可选) - 分页的数量.
offset
(可选) - 仅用于 params.offset 为空.
来源
Show Source
def paginate = { attrs ->
def writer = out
if(attrs.total == null)
throwTagError("Tag [paginate] is missing required attribute [total]") def messageSource = grailsAttributes.getApplicationContext().getBean("messageSource")
def locale = RCU.getLocale(request) def total = attrs.total.toInteger()
def action = (attrs.action ? attrs.action : (params.action ? params.action : "list"))
def offset = params.offset?.toInteger()
def max = params.max?.toInteger()
def maxsteps = (attrs.maxsteps ? attrs.maxsteps.toInteger() : 10) if(!offset) offset = (attrs.offset ? attrs.offset.toInteger() : 0)
if(!max) max = (attrs.max ? attrs.max.toInteger() : 10) def linkParams = [offset:offset - max, max:max]
if(params.sort) linkParams.sort = params.sort
if(params.order) linkParams.order = params.order
if(attrs.params) linkParams.putAll(attrs.params) def linkTagAttrs = [action:action]
if(attrs.controller) {
linkTagAttrs.controller = attrs.controller
}
if(attrs.id!=null) {
linkTagAttrs.id = attrs.id
}
linkTagAttrs.params = linkParams // determine paging variables
def steps = maxsteps > 0
int currentstep = (offset / max) + 1
int firststep = 1
int laststep = Math.round(Math.ceil(total / max)) // display previous link when not on firststep
if(currentstep > firststep) {
linkTagAttrs.class = 'prevLink'
linkParams.offset = offset - max
writer << link(linkTagAttrs.clone()) {
(attrs.prev ? attrs.prev : messageSource.getMessage('paginate.prev', null, messageSource.getMessage('default.paginate.prev', null, 'Previous', locale), locale))
}
} // display steps when steps are enabled and laststep is not firststep
if(steps && laststep > firststep) {
linkTagAttrs.class = 'step' // determine begin and endstep paging variables
int beginstep = currentstep - Math.round(maxsteps / 2) + (maxsteps % 2)
int endstep = currentstep + Math.round(maxsteps / 2) - 1 if(beginstep < firststep) {
beginstep = firststep
endstep = maxsteps
}
if(endstep > laststep) {
beginstep = laststep - maxsteps + 1
if(beginstep < firststep) {
beginstep = firststep
}
endstep = laststep
} // display firststep link when beginstep is not firststep
if(beginstep > firststep) {
linkParams.offset = 0
writer << link(linkTagAttrs.clone()) {firststep.toString()}
writer << '<span class="step">..</span>'
} // display paginate steps
(beginstep..endstep).each { i ->
if(currentstep == i) {
writer << "<span class=\"currentStep\">${i}</span>"
}
else {
linkParams.offset = (i - 1) * max
writer << link(linkTagAttrs.clone()) {i.toString()}
}
} // display laststep link when endstep is not laststep
if(endstep < laststep) {
writer << '<span class="step">..</span>'
linkParams.offset = (laststep -1) * max
writer << link(linkTagAttrs.clone()) { laststep.toString() }
}
} // display next link when not on laststep
if(currentstep < laststep) {
linkTagAttrs.class = 'nextLink'
linkParams.offset = offset + max
writer << link(linkTagAttrs.clone()) {
(attrs.next ? attrs.next : messageSource.getMessage('paginate.next', null, messageSource.getMessage('default.paginate.next', null, 'Next', locale), locale))
}
} }