Reputation: 11307
I have a service which returns a list of domain class objects to a controller. I'm looking for something to take pagination parameters and paginate this list like MyDomain.list(params)
does. Any suggestions?
Upvotes: 0
Views: 2088
Reputation: 310
Pass pagination params to your service method. If you do pagination after retrieving objects from database (in controller in your case), your query will still return many objects you don't need and it can become a performance issue.
Otherwise if you still want to have pagination without gorm features, then you can just slice your list i.e. def sliceList = list[5..10]
Upvotes: 4
Reputation: 10239
Do you want the pagination to take place in your controller, or in your service?
If you want to paginate in controller, it seems easy to do:
def myList = service.listItems()
if (params.sort)
myList = myList.sort {it."${params.sort}"}
if (params.order == "desc")
myList = myList.reverse()
int from = params.offset ?: 0
int to = from + (params.max ?: DEFAULT_SIZE)
myList = myList.subList(from, to)
If you want to paginate in service (for example, to not have to fetch all the rows from the service every time), you would have to move the pagination logic there. How would you implement it is dependent of what the service does; if it fetches the data by way of SQL statements, you would convert pagination params to directives like 'limit' and 'order by', etc.
Upvotes: 2