I am trying to explain the simple mechanism used in grails pagination . For this I have created brand new application which uses the grails default pagination and column sorting.In my grails-app I just used a domain class as follows:

class Book {
String title
String author
String type
static constraints = {
   title nullable: false,blank: false
   author nullable: true
   type nullable: true
 }
}

Corresponding to this domain I used grails scaffolding to generate Controller and Views.

Pagination

pagination
Pagination(Click on image to enlarge )

g:paginate tag uses total attribute to find number of links to generate, the generated links are actually back href to list action with params offset and max.list action is as follows:

def list(Integer max) {
params.max = Math.min(max ?: 5, 100)
List books = Book.list(params)
[bookInstanceList: books, total: Book.count()]
}
  • When first time this list action is called no max is there and hence 5 is set by max ?: 5 expression, now onwards on each call of this list action(number of links on page) max is 5 but offset will be changed accordingly, offset and number of links calculated in grails taglib (RenderTagLib) based on max and total.
  • Max determines the page size, and offset determines where the current page starts. The controller ‘s list action receives these parameters and generally passes them to a database query.The list method added to domain objects by grails handles these parameters and returns a result set starting at the given offset, with one page length.