Search code examples
htmlobjectgrailshtml-tablegsp

How to display a final range of show pages in Grails?


I have domain class objects being added to my database dynamically.

There is the generated index.gsp page which has the table of objects and I want to only display the final 50 objects, NOT all of them.

As of right now this is how the table looks:

<tbody>
<g:each in="${eventInstanceList}" status="i" var="eventInstance">
    <tr class="${(i % 2) == 0 ? 'even' : 'odd'}">

        <td><g:link action="show" id="${eventInstance.id}">${fieldValue(bean: eventInstance, field: "bandsintown_id")}</g:link></td>

        <td>${fieldValue(bean: eventInstance, field: "artist")}</td>

        <td>${fieldValue(bean: eventInstance, field: "ticketStatus")}</td>

        <td>${fieldValue(bean: eventInstance, field: "ticket_url")}</td>

        <td>${fieldValue(bean: eventInstance, field: "venue")}</td>

    </tr>
</g:each>

So to reiterate, the code above displays all of my objects in a table - I want to only display the last 50, can anyone help me out here?

UPDATE:

Got it to work with this code in my controller:

def index() 
{
    int eventCount = Event.count()
    int startingPoint = eventCount - 50

    def events = Event.createCriteria().list
    {
        order('id')
        firstResult(startingPoint)
        maxResults(50)
    }

    respond events
}

Solution

  • You can modify the index action in your controller, something like:

    def index() {
        def events = Events.createCriteria().list{
            order( 'id', 'desc' )
            maxResults( 50 )
        }
        respond events
    }
    

    You probably have eventInstanceCount being returned by index and used in your gsp pagination which you can remove.