Filtering a collection with offset in grails

谁都会走 提交于 2019-12-11 04:53:33

问题


I'm trying to filter a collection in grails with findAll so I only get the instances with a certain value in his field "estado".
I have something like this:

trabajos.findAll({it.estado.equals( "Pago")})

The problem is I dont know how to paginate the returned collection.
I took a look at grails documentation and found this

Book.findAll(Map queryParams, Closure whereCriteria)

but when I try it

trabajos.findAll([offset: 0], {it.estado.equals("Pago")})

I get the following exception

No signature of method: java.util.ArrayList.findAll() is applicable for argument types:       (java.util.LinkedHashMap, com.publidirecta.PersonalController$_show_closure2) values: [[offset:0], com.publidirecta.PersonalController$_show_closure2@a6bdb0] 
Possible solutions: findAll(), findAll(), findAll(groovy.lang.Closure), findAll(groovy.lang.Closure), find(), find()`  

Is this a why to achieve some sort of pagination this why or would I have to do it manually?


回答1:


I guess you are muss 2 things: grails and groovy.
Book.findAll(Map queryParams, Closure whereCriteria) - is a finder in database, it's grails thing and you can pass max param in it.
But in trabajos.findAll({it.estado.equals( "Pago")}) you are trying to find in list. It's groovy thing. See more details here and here

If you try to get objects from database, use like this

Book.findAll(Map queryParams, Closure whereCriteria).

If it's a list of objects use something like this:

def filtered = trabajos.findAll({it.estado.equals( "Pago")})
def result = filteredList[offset..offset+max < filtered.size() ? offset+max : filtered.size()]


来源:https://stackoverflow.com/questions/15899719/filtering-a-collection-with-offset-in-grails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!