Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Max and offset in Grails?

I have some list data and I want to do pagination. But I don't understand how to use 'max'. Here is my simple query code,

def users = User.where{
  roles {
     id in roles.id 
  }
}.list(max: 1)
println users
  • When I set max to 1, it shows 1 data,
  • I set max to 2, it shows 2 data,
  • I set max to 3, it shows 2 data,
  • I set max to 4, it shows 3 data and
  • I set max to 5, it shows 4 data

And I didn't understand what is offset. If I want show 5 data per page, What is my offset should be ?

like image 552
Hikaru Shindo Avatar asked Nov 01 '22 04:11

Hikaru Shindo


1 Answers

Write like this one, I took it from official docs:

def users = User.createCriteria().list (max: 10, offset: 10) {
    roles {
        'in'('id', roles*.id) 
    }
}

link to createCriteria docs You can take on controller params.offset and params.max and use it in those criteria. For example:

def users = User.createCriteria().list (max: params.max, offset: param.offset) {...}

I think you can add some links under list of elements with offset what you want. Good luck :)

like image 197
Koloritnij Avatar answered Nov 10 '22 14:11

Koloritnij