typically you have the following last line in a Grails generated Controller list method:
[userInstanceList: User.list(params), userInstanceTotal: User.count()]
if I want to restrict the users by a specific criteria it looks like
[userInstanceList: User.list(params) {
ilike('login', '%test%')
}, userInstanceTotal: User.count() {
ilike('login', '%test%')
}]
but this violates the DRY principle - what would be the best way to reuse the criteria closure?
Paged results from a criteria builder (the result class is PagedResultList) have a property, totalCount
, which you can use just like you were calling count()
on the same criteria:
def userInstanceList = User.createCriteria().list(params) {
ilike 'login', '%test%'
}
[userInstanceList: userInstanceList, userInstanceTotal: userInstanceList.totalCount]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With