{"id":1689,"date":"2010-09-14T21:38:42","date_gmt":"2010-09-14T16:08:42","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=1689"},"modified":"2010-09-15T13:44:12","modified_gmt":"2010-09-15T08:14:12","slug":"grails-pagination-on-filtered-list","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/grails-pagination-on-filtered-list\/","title":{"rendered":"Grails pagination on a ArrayList"},"content":{"rendered":"<p>Hi all,<br \/>\n<br \/>\nIn my recent grails project, i needed to paginate on an array list, so I wrote a function and thought would share it with you all.<br \/>\n<\/p>\n<pre lang=\"groovy\">\r\n\r\npublic List getFilteredList(int max, int offset) {\r\n    max = Math.min(max ?: 25, 100)\r\n    offset = (offset && offset>0) ?: 0\r\n\r\n    List names = getNames() \/\/Loads the complete list\r\n    int total = names.size()\r\n    int upperLimit = findUpperIndex(offset, max, total)\r\n    List filteredNames = names.getAt(offset..upperLimit)\r\n    return filteredNames\r\n  }\r\n\r\n  public static int findUpperIndex(int offset, int max, int total) {\r\n    max = offset + max - 1\r\n    if (max >= total) {\r\n      max -= max - total + 1\r\n    }\r\n    return max\r\n  }\r\n<\/pre>\n<p>So now if offset=20 and max=10, total = 28 so this will generate a list from 21st to 28th elements of the main list.<\/p>\n<p>\nHope this helps.<\/p>\n<p>\nVishal Sahu<br \/>\nSoftare Developer<br \/>\nvishal@intelligrape.com<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hi all, In my recent grails project, i needed to paginate on an array list, so I wrote a function and thought would share it with you all. public List getFilteredList(int max, int offset) { max = Math.min(max ?: 25, 100) offset = (offset &#038;&#038; offset>0) ?: 0 List names = getNames() \/\/Loads the complete [&hellip;]<\/p>\n","protected":false},"author":19,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":18},"categories":[7],"tags":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/1689"}],"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\/19"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=1689"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/1689\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=1689"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=1689"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=1689"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}