Sunday, August 22, 2010

Paginations to list

Grails has a great way of paginating domain object lists. It is just build in an works well.
But I had a list object that I needed to pagination, so I found this great post

List.metaClass.paginate = { max, offset=0 ->
  delegate.subList( offset, Math.min( offset + max, delegate.size() ) )
}
 
def list = (1..30).toList()
assert list == [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 
  18, 19, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30]
assert list.paginate( 5, 7 ) == [8, 9, 10, 11, 12]
assert list.paginate( 5 )    == [1, 2, 3, 4, 5]
assert list.paginate( 5, 0 ) == [1, 2, 3, 4, 5]
assert list.paginate( 5, 1 ) == [2, 3, 4, 5, 6]

No comments:

Post a Comment