Another approach for this situation (the need to reuse the same logic in different requests of different criteria) could be to use groovy closures, as indicated in the header of your message.
Your specific case is better solved using the totalCount property for the PagedResultList, just like Dana posted in her answer. But you can come up with a more complex scenario when you need to reuse logic inside criteria. For a similar situation, I have successfully tried the following solution. Using your case as an example:
def iLikeLoginClosure = { builder -> builder.ilike('login', '%test%') } [ userInstanceList: User.list(params) { iLikeLoginClosure delegate }, userInstanceTotal: User.count() { iLikeLoginClosure delegate } ]
Where is the builder in iLikeLoginClosure - a reference to the criteria builder object in which the closure is called. In addition, inside iLikeLoginClosure, you can use variables of the same scope within the closure.
This was inspired by this article: http://mrhaki.blogspot.com.ar/2010/06/grails-goodness-refactoring-criteria.html from MrHaki's excellent blog.
source share