Grails : Mutliple Paginations Using g:paginate tag

23 / Apr / 2009 by Amit Jain 1 comments

In my project, we needed a support for multiple paginations on the same page. For the same, we maintained the state of offset of different pagination tags and let tag use its specific offset rather than the general i.e from param.offset.

Assuming, we need two paginations on the same page. Lets assume the scenario where we have a movies.gsp page, where in I need to list hollywood movies and bollywood movies and both supports pagination. Insert the followings tags in the gsp page.

   
     

   
     

In the above code, apart from the regular parameters we have passed 2 new paramets in a params map. For example, In hollywood movies section tag ‘currentOffset’ indicates the tag has requested the change in page and otherOffset holds a value of the offset in the bollywood tag. We need to pass these extra parameters to maintain the state of the other pagination tag. And we get the values of offset i.e offset1/offset2 from the the action
Now in the controller, lets write the following code:

def movies = {
        //offset would have the value of the current pagination tag
        Integer offset= (params.offset) ? params.offset.toInteger() : 0
        Integer max= (params.max) ? params.max.toInteger() : 10
        int offset2, offset1
        int total = Movie.findWhere(...) //provide criteria to get the total of matching movies 

        //maitaining the state of offset of other pagination tags
        if(params.currentOffset == 'offset1'){
            offset2 = (params.otherOffset) ? params.otherOffset.toInteger() : 0
            offset1 = offset
        } else {
            offset1 =  (params.otherOffset) ? params.otherOffset.toInteger() : 0
            offset2 = offset
        }

        params.remove("offset") // offset removed from params so that pagination refers to the offset specified explicitly

       //to get the page specifc list
        def moviesList = Movie.withCriteria {
           ...	//give the criteria/condions here
        maxResults max
        firstResult offset
    }
     render(view: 'viewPath', model: [offset1: offset1, offset2: offset2 , moviesList: moviesList], total:total)
  }

Hope this helped.

~~Amit Jain~~
amit@intelligrape.com
http://www.IntelliGrape.com

FOUND THIS USEFUL? SHARE IT

comments (1 “Grails : Mutliple Paginations Using g:paginate tag”)

  1. nandini

    I am using the code u have put,
    Thanks,
    But it doesnt seem to work exactly the same.
    If i use params.remove(“offset”) the pagination doesnt work.
    If I remove it both lists paginate together.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *