views:

23

answers:

1

I'm trying to paginate through 2 tables on 1 gsp using 2 g:paginate tags. Hitting the paginate button on 1 table paginates both tables because both paginate tags are using the same 'max' and 'offset' params. How can I paginate through 1 table without paginating the other table?

Thanks in advance.

A: 

Here's an example using extra params on the paginate tag. Foo and Bar are two domain classes with a String property 'name'. I created 50 of each in Bootstrap.groovy.

PageController.groovy:

class PageController {

  def index = {
    if (params.paginate == 'Foo') {
      def fooPagination = [max: params.max, offset: params.offset]
      session.fooPagination = fooPagination
    } else if (params.paginate == 'Bar') {
      def barPagination = [max: params.max, offset: params.offset]
      session.barPagination = barPagination
    }
    def barList = Bar.list(session.barPagination ?: [max: 10, offset: 0])
    def fooList = Foo.list(session.fooPagination ?: [max: 10, offset: 0])
    //This is to stop the paginate using params.offset/max to calculate current step and use the offset/max attributes instead    
    params.offset = null
    params.max = null
    [fooList: fooList, totalFoos: Foo.count(), totalBars: Bar.count(), barList: barList]
  }
}

index.gsp:

<html>
<head>
  <title>Multi Pagination Example</title>

  <meta name="layout" content="main"/>
  <style type="text/css" media="screen">

  h2 {
    margin-top: 15px;
    margin-bottom: 15px;
    font-size: 1.2em;
  }
  </style>

</head>
<body>
<table>
  <tr>
    <td>
      <h2>Foo</h2>
      <table>
        <tr>
          <th>Name</th>
        </tr>
        <g:each in="${fooList}">
          <tr><td>${it.name}</td></tr>
        </g:each>
        <tr>
          <td class="paginateButtons">
            <g:paginate total="${totalFoos}" max="10" offset="${session.fooPagination?.offset}" params="${[paginate:'Foo']}"/></td>
        </tr>
      </table>
    </td>
    <td>
      <h2>Bar</h2>
      <table>
        <tr>
          <th>Name</th>
        </tr>
        <g:each in="${barList}">
          <tr><td>${it.name}</td></tr>
        </g:each>
        <tr>
          <td class="paginateButtons">
            <g:paginate total="${totalBars}" max="10" offset="${session.barPagination?.offset}" params="${[paginate:'Bar']}"/></td>
        </tr>
      </table>
    </td>
  </tr>
</table>

</body>
</html>

cheers

Lee

leebutts