{"id":1260,"date":"2010-07-14T22:48:26","date_gmt":"2010-07-14T17:18:26","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=1260"},"modified":"2017-01-05T09:30:49","modified_gmt":"2017-01-05T04:00:49","slug":"criteria-query-and-pagination-params","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/criteria-query-and-pagination-params\/","title":{"rendered":"Criteria Query and pagination params"},"content":{"rendered":"<p>I have been using the following code to get paginated result and the total number of results returned irrespective of the pagination params.<\/p>\n<div class=\"code\">\n<pre lang=\"groovy\"> def result=SampleDomain.createCriteria().list(){\r\n\/\/ multiple restrictions\r\n   maxResults(params.max)\r\n   firstResult(params.offset)\r\n} \/\/ Return type is ArrayList\r\n\r\nInteger  totalResult=SampleDomain.createCriteria().count(){\r\n\/\/ multiple restrictions\r\n\/\/ maxResults(params.max)\r\n\/\/ firstResult(params.offset)\r\n}<\/pre>\n<\/div>\n<p>Clearly duplicating the same closure except for the pagination restrictions was not a good solution.<\/p>\n<p>After a little googling and reading mailing lists I got the solution.\u00a0Passing pagination params to createCriteria.list() returns result of type PagedResultList which provides many useful methods. The getTotalCount() method of PagedResultList class returns the actual number of results returned irrespective of the pagination restrictions(maxResults and firstResult). Also the result contains only those records fulfiling maxResults and firstResult restrictions.<\/p>\n<div class=\"code\">\n<pre lang=\"groovy\">def result=SampleDomain.createCriteria().list(max:params.max, offset:params.offset){\r\n\/\/ multiple\/complex restrictions\r\n   maxResults(params.max)\r\n   firstResult(params.offset)\r\n} \/\/ Return type is PagedResultList<\/pre>\n<\/div>\n<p>Thanks to all the active users of Grails mailing list.<\/p>\n<p>Here are few useful links:<\/p>\n<p>Bhagwat Kumar<br \/>\nbhagwat(at)intelligrape(dot)com<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I have been using the following code to get paginated result and the total number of results returned irrespective of the pagination params. def result=SampleDomain.createCriteria().list(){ \/\/ multiple restrictions maxResults(params.max) firstResult(params.offset) } \/\/ Return type is ArrayList Integer totalResult=SampleDomain.createCriteria().count(){ \/\/ multiple restrictions \/\/ maxResults(params.max) \/\/ firstResult(params.offset) } Clearly duplicating the same closure except for the pagination [&hellip;]<\/p>\n","protected":false},"author":13,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":6},"categories":[7],"tags":[266,320,276,322,321],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/1260"}],"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\/13"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=1260"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/1260\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=1260"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=1260"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=1260"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}