{"id":48,"date":"2009-04-23T15:30:23","date_gmt":"2009-04-23T10:00:23","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=48"},"modified":"2016-12-19T15:19:21","modified_gmt":"2016-12-19T09:49:21","slug":"grails-mutliple-paginations","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/grails-mutliple-paginations\/","title":{"rendered":"Grails : Mutliple Paginations Using g:paginate tag"},"content":{"rendered":"<p>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 <g:pagination> tag use its specific offset rather than the general i.e from param.offset. <\/g:pagination><\/p>\n<p>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.<\/p>\n<pre lang=\"groovy\">\r\n   <!--Hollywood movies section -->\r\n     <g:paginate offset=\"${offset1}\" params=\"['currentOffset':'offset1','otherOffset':otherOffset]\" next=\"&gt;&gt;\" prev=\"&lt;&lt;\" total=\"${total}\">\r\n\r\n   <!-- Bollywood movies section -->\r\n     <g:paginate offset=\"${offset2}\" params=\"['currentOffset':'offset2','otherOffset':otherOffset]\" next=\"&gt;&gt;\" prev=\"&lt;&lt;\" total=\"${total}\">\r\n<\/g:paginate><\/g:paginate><\/pre>\n<p>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 &#8216;currentOffset&#8217; 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<br \/>\nNow in the controller, lets write the following code:<\/p>\n<pre lang=\"groovy\">\r\ndef movies = {\r\n        \/\/offset would have the value of the current pagination tag\r\n        Integer offset= (params.offset) ? params.offset.toInteger() : 0\r\n        Integer max= (params.max) ? params.max.toInteger() : 10\r\n        int offset2, offset1\r\n        int total = Movie.findWhere(...) \/\/provide criteria to get the total of matching movies \r\n\r\n        \/\/maitaining the state of offset of other pagination tags\r\n        if(params.currentOffset == 'offset1'){\r\n            offset2 = (params.otherOffset) ? params.otherOffset.toInteger() : 0\r\n            offset1 = offset\r\n        } else {\r\n            offset1 =  (params.otherOffset) ? params.otherOffset.toInteger() : 0\r\n            offset2 = offset\r\n        }\r\n\r\n        params.remove(\"offset\") \/\/ offset removed from params so that pagination refers to the offset specified explicitly\r\n\r\n       \/\/to get the page specifc list\r\n        def moviesList = Movie.withCriteria {\r\n           ...\t\/\/give the criteria\/condions here\r\n        maxResults max\r\n        firstResult offset\r\n    }\r\n     render(view: 'viewPath', model: [offset1: offset1, offset2: offset2 , moviesList: moviesList], total:total)\r\n  }<\/pre>\n<p>Hope this helped.<\/p>\n<p>~~Amit Jain~~<br \/>\namit@intelligrape.com<br \/>\n<a href=\"http:\/\/www.tothenew.com\">http:\/\/www.IntelliGrape.com<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 [&hellip;]<\/p>\n","protected":false},"author":7,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":5},"categories":[7],"tags":[4840,9,80,79,83],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/48"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=48"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/48\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=48"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=48"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=48"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}