Question

I'm newbie to grails and I've tried to use the grails pagination Tag found in grails documentation , i have a search form in my index.gsp

i don't know how to pass params.max and params.offset i am using only one action index

i have an index action containing the query to list some books :

params.max = 10
params.offset = 0
def author=params.author // i get the input search params
def max = 10
def offset = 0
if(params.max){
    max = params.max
}
if(params.offset){
    offset = params.offset
}

def bookPagin=Book.createCriteria().list {      
    eq("author", author)}
    maxResults(max)
    firstResult(offset )
}

this is the paginate tag in my index.gsp :

 <g:paginate controller="displayBook" action="index" total="${bookPagin.size()}" params="[author:author]"/>

now this code displays the first 10 results , but when i click on the second page it doesn't take the input author params although i am pasing the author param in the GET request , is there any other solution ?

Was it helpful?

Solution

Here is how I would do it...

def searchString = "${params.author}%"
def searchResults = Book.findAllByAuthorLike(searchString, params) // max, offset automatically handled
def total = Book.countByAuthorLike(searchString)
render (model:[searchResults: searchResults, total: total])

in your GSP, iterate over your searchResults with:

<g:each var="book" in="${searchResults}">...

and after that include:

 <g:paginate controller="displayBook" action="index" total="${total}"/>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top