【问题标题】:Grails pagination isn't working properlyGrails 分页无法正常工作
【发布时间】:2015-04-16 12:08:14
【问题描述】:

我是 Grails 的新手,我尝试使用文档中的分页标签来显示特定数据。

分页显示在我的页面上,但是当我滑过它们时,它会在每个页面上显示我的所有数据,我希望每页仅显示总和中的 10 个项目。

这是我的控制器

class HomeController {

    def actions() {
       params.max = Math.min(params.max ? params.int('max') : 10, 100)

       def articleActionOpera = newSuggestedShoppingItemsService.getNewSuggestedShoppingItems()

       return [articles: articleActionOpera, actionTotal: articleActionOpera.size(), params:params]
    }
}

这是我的服务

class NewSuggestedShoppingItemsService {

   def operaOpusDbConnectorService

   def getNewSuggestedShoppingItems() {

      def returnRows = null
      def sqlInstance = operaOpusDbConnectorService.openOperaOpusConnection()

      try {
         def sqlQuery = "SELECT passW, ArtName, Descript FROM dbo.fnWS_Newarticles()"

         returnRows = sqlInstance.rows(sqlQuery)

         return returnRows
      }
      catch(Exception ex) {
         log.error("Fail, item are not shown.")
         throw new Exception (ex)
      }
   }
}

还有我的 .gsp

...

<div id="paging">
   <g:paginate total="${actionTotal}" />
</div>

【问题讨论】:

    标签: grails pagination


    【解决方案1】:

    您实际上并没有将任何分页参数(最大和偏移量)传递给您的数据库查询,从而获取所有记录。因此,将它们传递给您的查询,这将起作用。

    第二件事,您将总计数作为返回结果的大小(其中值正确但编程错误)和最大/偏移量传递给 Grails 分页指令,即为什么它呈现正确的分页 HTML sn-p。

    例子:

    class HomeController {
    
        def actions(Integer max) {
           params.max = Math.min(max ?: 10, 100)
    
           List articleActionOpera = newSuggestedShoppingItemsService.getNewSuggestedShoppingItems()
    
           // You don't have to pass params in model, it is bydefault available in GSPs
           return [articles: articleActionOpera, actionTotal: articleActionOpera.totalCount]
        }
    }
    

    服务:

    class NewSuggestedShoppingItemsService {
    
       List getNewSuggestedShoppingItems(Map params) {
          // Consider ShoppingItem is a domain class and params is containing max & 
          // offset parameters then it will return an instance of PagedResultList (List)
          // which will have a totalCount field
    
          List shoppingItems = ShoppingItem.createCriteria().list(params) {
               // Your criteria
               eq("status", "ACTIVE")
          }
    
    
          return shoppingItems
       }
    }
    

    在此处阅读更多信息http://grails.github.io/grails-doc/latest/ref/Domain%20Classes/createCriteria.html

    【讨论】:

    • 你能发布一些代码示例或解释如何为你提到的事情做这件事吗?
    • 嗯,这是 Grails 中最常见的事情。我已经用一个例子更新了答案。
    • 对不起,这对我不起作用。这是我得到的错误 No such property: ShoppingItem for class: hr.svam.web.operaopus.NewSuggestedShoppingItemsService。 Stacktrace 如下:消息:没有这样的属性:ShoppingItem 类:hr.svam.web.operaopus.NewSuggestedShoppingItemsService
    • 我已经在示例代码中提到我正在使用示例域。我真的没想到你会复制粘贴我的例子。请准备好 Grails 域类。
    【解决方案2】:

    嗨,Damir 使用下面的说明,您将很容易在 grails 中实现分页:

    查看您的代码是可以的:(无需更改)

    <div id="paging">
       <g:paginate total="${actionTotal}" />
    </div>
    

    在控制器中:

    class HomeController {
    
        def actions(Integer max) {
           params.max = Math.min(max ?: 10, 100)
    //change here
           List articleActionOpera = newSuggestedShoppingItemsService.getNewSuggestedShoppingItems(params)
    
           // You don't have to pass params in model, it is bydefault available in GSPs
           return [articles: articleActionOpera, actionTotal: articleActionOpera.totalCount,params:params]
        }
    }
    

    在服务中使用创建标准:

    class NewSuggestedShoppingItemsService {
    
       def operaOpusDbConnectorService
    
       def getNewSuggestedShoppingItems(def params) {
    
          def returnRows = null
       /*   def sqlInstance = operaOpusDbConnectorService.openOperaOpusConnection()
    
          try {
             def sqlQuery = "SELECT passW, ArtName, Descript FROM dbo.fnWS_Newarticles()"
    
             returnRows = sqlInstance.rows(sqlQuery)
    
             return returnRows
          }
          catch(Exception ex) {
             log.error("Fail, item are not shown.")
             throw new Exception (ex)
          }*/
    
    returnRows=YourDomainName.createCriteria().list(params) {
     projections {
            property("passW")
            property("ArtName")
            property("Descript")
        }
    }
    
       }
    }
    

    注意:必须更改 YourDomainName == 映射到表的类的名称 它会正常工作。

    【讨论】:

    • 所以我已经按照你说的做了,但现在我收到了这个提示的错误,Class org.hibernate.QueryException Message could not resolve property: passW of: hr.svam.web.operaopus。文章
    • property("passW") // 使用映射到您表中的 passW 的域属性更改 passW。 property("ArtName")//对这三个属性都做上面的事情。 property("Descript") 这 3 个属性实际上是您的域类中存在的列名到表中。所以用域类字段替换它们。
    • 如你所说,我已将数据映射到域类。但我仍然遇到错误。类 org.codehaus.groovy.runtime.typehandling.GroovyCastException 消息无法将类“java.lang.Boolean”的对象“false”转换为“java.util.List”类这是什么问题??
    • 发送你的完整代码和你的错误,这只是一个类型转换错误。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-14
    • 2014-09-21
    • 2013-08-22
    • 2012-09-25
    • 2016-07-29
    • 2017-11-27
    相关资源
    最近更新 更多