Last active
October 17, 2016 17:34
-
-
Save pablocloud/fc0e81c547f14206d1da5f51e319d47e to your computer and use it in GitHub Desktop.
This was used to use materialize with Grails pagination, tag is g:pagination
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package swimpee | |
import grails.util.TypeConvertingMap | |
import grails.web.mapping.UrlMapping | |
import org.grails.taglib.TagOutput | |
import org.grails.taglib.encoder.OutputContextLookupHelper | |
import org.springframework.web.servlet.support.RequestContextUtils | |
class PaginationTagLib { | |
def pagination = { Map attrsMap -> | |
TypeConvertingMap attrs = (TypeConvertingMap)attrsMap | |
def writer = out | |
if (attrs.total == null) { | |
throwTagError("Tag [paginate] is missing required attribute [total]") | |
} | |
def messageSource = grailsAttributes.messageSource | |
def locale = RequestContextUtils.getLocale(request) | |
def total = attrs.int('total') ?: 0 | |
def offset = params.int('offset') ?: 0 | |
def max = params.int('max') | |
def maxsteps = (attrs.int('maxsteps') ?: 10) | |
if (!offset) offset = (attrs.int('offset') ?: 0) | |
if (!max) max = (attrs.int('max') ?: 10) | |
Map linkParams = [:] | |
if (attrs.params instanceof Map) linkParams.putAll((Map)attrs.params) | |
linkParams.offset = offset - max | |
linkParams.max = max | |
if (params.sort) linkParams.sort = params.sort | |
if (params.order) linkParams.order = params.order | |
Map linkTagAttrs = [:] | |
def action | |
if (attrs.containsKey('mapping')) { | |
linkTagAttrs.mapping = attrs.mapping | |
action = attrs.action | |
} else { | |
action = attrs.action ?: params.action | |
} | |
if (action) { | |
linkTagAttrs.action = action | |
} | |
if (attrs.controller) { | |
linkTagAttrs.controller = attrs.controller | |
} | |
if (attrs.containsKey(UrlMapping.PLUGIN)) { | |
linkTagAttrs.put(UrlMapping.PLUGIN, attrs.get(UrlMapping.PLUGIN)) | |
} | |
if (attrs.containsKey(UrlMapping.NAMESPACE)) { | |
linkTagAttrs.put(UrlMapping.NAMESPACE, attrs.get(UrlMapping.NAMESPACE)) | |
} | |
if (attrs.id != null) { | |
linkTagAttrs.id = attrs.id | |
} | |
if (attrs.fragment != null) { | |
linkTagAttrs.fragment = attrs.fragment | |
} | |
linkTagAttrs.params = linkParams | |
// determine paging variables | |
def steps = maxsteps > 0 | |
int currentstep = ((offset / max) as int) + 1 | |
int firststep = 1 | |
int laststep = Math.round(Math.ceil(total / max)) as int | |
writer << '<div class="pagination center"><ul>'; | |
// display previous link when not on firststep unless omitPrev is true | |
if (currentstep > firststep && !attrs.boolean('omitPrev')) { | |
writer << '<li class="waves-effect">'; | |
linkParams.offset = offset - max | |
writer << callLink((Map)linkTagAttrs.clone()) { | |
(attrs.prev ?: messageSource.getMessage('paginate.prev', null, messageSource.getMessage('default.paginate.prev', null, 'Previous', locale), locale)) | |
} | |
writer << '</li>'; | |
} | |
// display steps when steps are enabled and laststep is not firststep | |
if (steps && laststep > firststep) { | |
// determine begin and endstep paging variables | |
int beginstep = currentstep - (Math.round(maxsteps / 2.0d) as int) + (maxsteps % 2) | |
int endstep = currentstep + (Math.round(maxsteps / 2.0d) as int) - 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 && !attrs.boolean('omitFirst')) { | |
writer << '<li class="waves-effect">'; | |
linkParams.offset = 0 | |
writer << callLink((Map)linkTagAttrs.clone()) {firststep.toString()} | |
writer << '</li>'; | |
} | |
// display paginate steps | |
(beginstep..endstep).each { int i -> | |
writer << '<li>'; | |
if (currentstep == i) { | |
writer << "<span class=\"btn blue-grey active'\">${i}</span>" | |
} | |
else { | |
linkParams.offset = (i - 1) * max | |
writer << callLink((Map)linkTagAttrs.clone()) {i.toString()} | |
} | |
writer << '</li>'; | |
} | |
} | |
// display next link when not on laststep unless omitNext is true | |
if (currentstep < laststep && !attrs.boolean('omitNext')) { | |
writer << '<li class="waves-effect">'; | |
linkParams.offset = offset + max | |
writer << callLink((Map)linkTagAttrs.clone()) { | |
(attrs.next ? attrs.next : messageSource.getMessage('paginate.next', null, messageSource.getMessage('default.paginate.next', null, 'Next', locale), locale)) | |
} | |
writer << '</li>'; | |
} | |
out << '</ul></div>'; | |
} | |
private callLink(Map attrs, Object body) { | |
TagOutput.captureTagOutput(tagLibraryLookup, 'g', 'link', attrs, body, OutputContextLookupHelper.lookupOutputContext()) | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Just check the changed lines from the defaults, my lines are ending with the uneeded semicolon.
It will produce the next output :