简体   繁体   中英

Grails pagination isn't working properly

I'm a newbie to Grails and I've tried to use the pagination Tag found in the documentation to show specific data.

Pagination is showing on my page but it displays all my data on every page when I slide through them and I would like to display only 10 items per page from total sum.

This is my controller

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

This is my service

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

and my .gsp

...

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

You are actually not passing any pagination parameters (max & offset) to your database query and hence getting all the records. So pass them to your query and this will work.

Second thing, you are passing the total count as the size of returned results (where value is correct but programmatically wrong) and max/offset to Grails pagination directive, ie why it is rendering proper pagination HTML snippet.

Example:

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

Service:

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

Read more here http://grails.github.io/grails-doc/latest/ref/Domain%20Classes/createCriteria.html

Hi Damir use below instructions and you will easily achieve pagination in grails :

On view your code is ok : (No changes needed)

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

In Controller :

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

In service use create criteria :

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

   }
}

Note : must change YourDomainName == Name of Class which maps to table it will work properly.

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