Search code examples
javagrails

Controlling the number of items on a page in Grails' scaffolding view


Is there any built-in way to allow the user to control the number of items to show simultaneously in Grails' scaffolding view? If not, what is the best way (Grails conventions-wise) of doing this?

Since the parameter max (like offset) is passed in the URL when using the default pagination and changing it is enough to have various list sizes work, I began to wonder if there's some built in way to allow the user to control it through interface? Right now, I'm using a naive brute-force approach of something like this in the generated index.gsp view:

    <a href="index?max=10">10</a>
    <a href="index?max=20">20</a>
    <a href="index?max=50">50</a>

...but I reckon this surely isn't the Grail-ish way of doing this but I couldn't find anything about either a built-in or a cleaner custom (in case of lack of the former) approach. As for the version, I'm using Grails 3.3.8.


Solution

  • There shurely be more grails-ish ways to do it. But I think this comes close.

    First use grails install-templates to install the Templates. Then edit the newly generated index.gsp template and add

    <g:if test="\${${propertyName}Count > 10}">
        <g:link action="index" params="\${params + [max:10]}">10</g:link>
        <g:link action="index" params="\${params + [max:20]}">20</g:link>
        <g:link action="index" params="\${params + [max:50]}">50</g:link>
    </g:if>
    

    somewhere, perhaps below the g:paginate tag.

    Other relevant links:
    grails generate-views
    The link tag in the gsp docs