如何使用Grails分页功能

4
我是一个Grails新手,尝试使用Grails文档中找到的分页标签,我在我的index.gsp页面中有一个搜索表单。
我不知道如何传递params.max和params.offset参数,因为我只使用了一个名为index的操作。
我有一个包含查询以列出一些书籍的index操作:
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 )
}

这是我 index.gsp 中的 paginate 标签:
 <g:paginate controller="displayBook" action="index" total="${bookPagin.size()}" params="[author:author]"/>

现在这段代码显示了前10个结果,但是当我点击第二页时,它没有使用输入的作者参数,尽管我在GET请求中传递了作者参数,是否有其他解决方案?


params属性对我来说看起来没问题。你还没有包括设置model属性的控制器方法的其余部分,所以最好检查一下author是不是正确的变量名——我通常使用[author:params.author]。顺便说一句,如果actioncontroller属性与当前页面相同(通常是这样),则可以省略它们。 - nickdos
1个回答

4

以下是我如何做到的……

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])

在您的GSP中,使用以下方式遍历searchResults
<g:each var="book" in="${searchResults}">...

并在此之后包含:
 <g:paginate controller="displayBook" action="index" total="${total}"/>

我必须使用“with criteria”,因为我有多个搜索参数,并且在索引操作中:我发送对象,例如authorList [authorList:authorList]。 - marcAntoine
在所有分页示例中,他们将参数从索引操作发送到另一个名为g:paginate的操作,但我只使用一个操作索引。 - marcAntoine
下次请附上您的实际代码。我们只能根据您在问题中提供的内容进行推断。 - nickdos

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接