Skip to content

Instantly share code, notes, and snippets.

@chenzhenli
Forked from akhawaja/Application.java
Created June 12, 2012 20:45
Show Gist options
  • Save chenzhenli/2920018 to your computer and use it in GitHub Desktop.
Save chenzhenli/2920018 to your computer and use it in GitHub Desktop.
Play! Framework 1.2.x Pagination (this is a variation and modification of the version found on https://gist.github.com/1379217)
package controllers;
import controllers.Constants;
import play.mvc.Controller;
import utils.PaginationInfo;
import java.util.List;
/**
* Sample controller.
*/
public class Application extends Controller {
public static void listBlogs() {
int currentPage = PaginationInfo.getCurrentPage();
List<models.Blog> blogs = models.Blog.all()
.limit(Constants.RECORDS_PER_PAGE).offset(currentPage - 1).asList();
Long total = models.Blog.all().count();
PaginationInfo paginationInfo = new PaginationInfo("Application.listBlogs",
currentPage, Constants.RECORDS_PER_PAGE, total.intValue());
render(blogs, paginationInfo);
}
}
*{ sample view }*
#{extends 'main.html'/}
<table>
<tbody>
#{list blogs, as:'blog'}
<tr>
<td>${blog.title}</td>
</tr>
#{/list}
#{pagination paginationInfo, colspan:'1'/}
</tbody>
</table>
# Pagination strings
pagination.pageNumbers=<strong>Found: %d &mdash; Page: %d of %d</strong>
pagination.next=Next
pagination.previous=Prev
pagination.first=First
pagination.last=Last
*{
View Tag. Put this in your tags folder.
Optional arguments:
- first: [<<] label for the "first" link
- last: [>>] label for the "last" link
- previous: [<] label for the "previous" link
- next: [>] label for the "next" link
- radius: [4] how many pages to display left/right of the current page
- clazz: [] additional css class for outer div, the "pagination" class is always added.
- colspan: [3] number of table rows to span.
}*
%{
def page = {
args = new java.util.HashMap<String, Object>()
args.put('page', it)
//args.put('zip', params.get( "zip" ) )
_arg.params.each{ p -> args.put(p, params.get(p)) }
play.mvc.Router.getFullUrl(_arg.action, args)
}
pageCount = _arg.getNumPages();
currentPage = _arg.getPage();
first = (_first == null || _first == '') ? '&laquo; ' + play.i18n.Messages.get("pagination.first") : _first;
previous = (_previous == null || _previous == '') ? '&lsaquo; ' + play.i18n.Messages.get("pagination.previous") : _previous;
next = (_next == null || _next == '') ? play.i18n.Messages.get("pagination.next") + ' &rsaquo;' : _next;
last = (_last == null || _last == '') ? play.i18n.Messages.get("pagination.last") + ' &raquo;' : _last;
radius = (_radius == null )? 3 : _radius;
startCount = Math.max(1, currentPage - radius);
endCount = Math.min(pageCount, currentPage + radius);
}%
<tr>
<td colspan="${_colspan == null ? '' : _colspan}">
<ul class="pagination${_clazz}">
<li class="pagination_info">
&{'pagination.pageNumbers', _arg.numItems, _arg.getPage(), pageCount}
</li>
%{
if (_arg.page > 1) {
out.println('<li><a class="first button" rel="1" href="' + page(1) +'">' + first + '</a></li>');
out.println('<li><a class="previous button" rel="' + (_arg.page - 1) + '" href="' + page(_arg.page - 1) + '">' + previous + '</a></li>');
}
if (startCount > 1) {
out.println('<li class="hellip">&nbsp;&hellip;&nbsp;</li>');
}
for (int i = startCount; i <= endCount; i++) {
if (i != _arg.getPage()) {
out.println('<li><a class="button" rel="' + i + '" href="' + page(i) + '">' + i + '</a></li>' );
} else {
out.println('<li><span class="current button gray">' + i + '</span></li>');
}
}
if ((currentPage + radius) < pageCount) {
out.println('<li class="hellip">&nbsp;&hellip;&nbsp;</li>');
}
if (_arg.page != pageCount) {
out.println('<li><a class="next button" rel="' + i + '" href="' + page(_arg.getPage() + 1) + '">' + next + '</a></li>');
out.println('<li><a class="last button" rel="' + i + '" href="' + page(pageCount) + '">' + last +'</a></li>');
}
}%
</ul>
</td>
</tr>
package utils;
import play.mvc.Scope;
import java.util.LinkedList;
import java.util.List;
/**
* Helps a bit with the paging.
* This class (as the name suggests) doesn't generate any html,
* but just holds information about the paging.
*
* @author hansi
* @author akhawaja
*/
public class PaginationInfo implements Cloneable {
// what's the current page? (starting at 1)
public int page;
// what's the action url (e.g. Application.listSomething)
public String action;
// what params from the current request should be included in the links?
// e.g. if you're paging over a search and your textfield is named "q",
// then you should call: paginationInfo.addParams( "q" ).
// this will take the current value from the request parameters and includ it
// in the link, so it's point to /application/listSomething?q=the_value&page=3
public LinkedList<String> params = new LinkedList<String>();
// number of items per page
public int perPage = 15;
// number of result items
public int numItems;
public static int getCurrentPage() {
Scope.Params parameters = Scope.Params.current();
Object o = parameters.get("page");
if (o == null) {
return 1;
} else {
try {
Integer.parseInt(String.valueOf(o));
} catch (NumberFormatException e) {
return 1;
}
}
return 1;
}
public PaginationInfo() {
}
public PaginationInfo(String action, int page, int perPage, int numItems, String... params) {
action(action).page(page).perPage(perPage).numItems(numItems).addParams(params);
}
/**
* Set the action (url, e.g. Application.doSomething)
* Allows method chaining
*/
public PaginationInfo action(String action) {
this.action = action;
return this;
}
/**
* Set the current page
* Allows method chaining
*/
public PaginationInfo page(int page) {
this.page = page;
return this;
}
/**
* Set the number of items visible per page
* Allows method chaining
*/
public PaginationInfo perPage(int perPage) {
this.perPage = perPage;
return this;
}
/**
* Set total number of result items
* Allows method chaining
*/
public PaginationInfo numItems(int numItems) {
this.numItems = numItems;
return this;
}
/**
* Add query parameters' names which should be included in links
* Allows method chaining
*/
public PaginationInfo addParams(String... params) {
for (String param : params) {
this.params.add(param);
}
return this;
}
/**
* Add query parameters' names which should be included in links
* Allows method chaining
*/
public PaginationInfo addParams(List<String> params) {
this.params.addAll(params);
return this;
}
/**
* Validates and returns the current page.
* This requires numItems and perPage to be set to function correctly.
*
* @return
*/
public int getPage() {
return Math.max(1, Math.min(page, getNumPages()));
}
/**
* Calculates and returns the total number of pages.
* This requires numItems and perPage to be set to function correctly.
*
* @return
*/
public int getNumPages() {
return 1 + (int) Math.floor((numItems - 1) / (float) perPage);
}
@Override
protected PaginationInfo clone() throws CloneNotSupportedException {
return new PaginationInfo(action, page, perPage, numItems).addParams(params);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment