【问题标题】:how to use grails pagination如何使用 grails 分页
【发布时间】:2014-04-09 09:13:05
【问题描述】:

我是 grails 的新手,我尝试使用 grails 文档中的 grails 分页标签,我的 index.gsp 中有一个搜索表单

我不知道如何传递 params.max 和 params.offset 我只使用一个动作索引

我有一个索引操作,其中包含列出一些书籍的查询:

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 中的分页标记:

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

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

【问题讨论】:

  • params 属性对我来说看起来不错。您还没有包括设置model 属性的控制器方法的其余部分,所以也许检查author 是正确的变量名——我通常使用[author: params.author]。仅供参考,如果 actioncontroller 属性与当前页面相同(通常它们是),您可以省略它们。

标签: java grails pagination criteria


【解决方案1】:

这就是我的做法......

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

【讨论】:

  • 我必须使用条件,因为我有多个搜索参数,并且在索引操作中:我正在发送对象,如 authorList [authorList: authorList]
  • 在所有分页示例中,他们将参数从索引操作发送到另一个名为 g:paginate 的操作,但我只使用一个操作索引
  • 下次包含您的实际代码。我们只能取消您在问题中提出的内容
【解决方案2】:

【讨论】:

  • 帮助不大
猜你喜欢
  • 2019-09-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-24
  • 2021-11-09
  • 1970-01-01
相关资源
最近更新 更多