Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I have a remote paginate tab, that helps me paginate results via ajax. Its an improvement over the default tab and takes in custom arguments.</p> <p>Here's the code:</p> <pre><code>class CustomRemotePaginateTagLib { static namespace = 'myTagLib' /** * Creates next/previous links to support pagination for the current controller * * &lt;g:paginate total="$ { Account.count() } " /&gt; */ def remotePaginate = {attrs -&gt; def writer = out if (attrs.total == null) throwTagError("Tag [remotePaginate] is missing required attribute [total]") if (attrs.update == null) throwTagError("Tag [remotePaginate] is missing required attribute [update]") def locale = RequestContextUtils.getLocale(request) def total = attrs.total.toInteger() def update = attrs.update def action = (attrs.action ? attrs.action : (params.action ? params.action : "list")) def controller = (attrs.controller ? attrs.controller : params.controller) 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) linkParams['action'] = action linkParams['controller'] = controller def linkTagAttrs = [url: "#"] if (attrs.controller) { linkTagAttrs.controller = attrs.controller } if (attrs.id != null) { linkTagAttrs.id = attrs.id } // determine paging variables def steps = maxsteps &gt; 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 &gt; firststep) { linkTagAttrs.class = 'prevLink' def prevOffset = linkParams.offset def params = attrs.params ?: [] params.'offset' = prevOffset linkTagAttrs.onclick = g.remoteFunction(update: update, action: linkParams.action, controller: linkParams.controller, params: params) writer &lt;&lt; link(linkTagAttrs.clone()) { (attrs.prev ? attrs.prev : g.message(code: 'default.paginate.prev', default: 'Previous')) } } // display steps when steps are enabled and laststep is not firststep if (steps &amp;&amp; laststep &gt; 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 &lt; firststep) { beginstep = firststep endstep = maxsteps } if (endstep &gt; laststep) { beginstep = laststep - maxsteps + 1 if (beginstep &lt; firststep) { beginstep = firststep } endstep = laststep } // display firststep link when beginstep is not firststep if (beginstep &gt; firststep) { linkParams.offset = 0 def params = attrs.params ?: [] params['offset'] = linkParams.offset linkTagAttrs.onclick = g.remoteFunction(update: update, action: linkParams.action, controller: linkParams.controller, params: params) writer &lt;&lt; link(linkTagAttrs.clone()) { firststep.toString() } writer &lt;&lt; '&lt;span class="step"&gt;..&lt;/span&gt;' } // display paginate steps (beginstep..endstep).each {i -&gt; if (currentstep == i) { writer &lt;&lt; "&lt;span class=\"currentStep\"&gt;${i}&lt;/span&gt;" } else { linkParams.offset = (i - 1) * max def params = attrs.params ?: [] params['offset'] = linkParams.offset linkTagAttrs.onclick = g.remoteFunction(update: update, action: linkParams.action, controller: linkParams.controller, params: params) writer &lt;&lt; link(linkTagAttrs.clone()) { i.toString() } } } // display laststep link when endstep is not laststep if (endstep &lt; laststep) { writer &lt;&lt; '&lt;span class="step"&gt;..&lt;/span&gt;' linkParams.offset = (laststep - 1) * max def params = attrs.params ?: [] params['offset'] = linkParams.offset linkTagAttrs.onclick = g.remoteFunction(update: update, action: linkParams.action, controller: linkParams.controller, params: params) writer &lt;&lt; link(linkTagAttrs.clone()) { laststep.toString() } } } // display next link when not on laststep if (currentstep &lt; laststep) { linkTagAttrs.class = 'nextLink' linkParams.offset = offset + max def params = attrs.params ?: [] params['offset'] = linkParams.offset linkTagAttrs.onclick = g.remoteFunction(update: update, action: linkParams.action, controller: linkParams.controller, params: params) writer &lt;&lt; link(linkTagAttrs.clone()) { (attrs.next ? attrs.next : g.message(code: 'default.paginate.next', default: 'Next')) } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload