Grails paginating multiple tables
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开发者_Python百科 the other table?
Thanks in advance.
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
You can also use remote-pagination plugin, which does exactly the same job. Cheers.
Grails pagination is completely useless for multiple pagination in one page bceause you can't pass in custom max and offset as attribute. Here is the reason why
def offset = params.int('offset') ?: 0
def max = params.int('max')
And documentation tells you "max (optional) - The number of records to display per page (defaults to 10). Used ONLY if params.max is empty" WHAT? What is the meaning of creating something that I can't control. Good programming way - Let me enter my variables or give me options, then use your default values!
精彩评论