简体   繁体   中英

how to use grails pagination

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 ?

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}"/>

Pagination with grails is very easy to implement. Please go through

You should be able to implement pagination now.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM