Skip to content

Instantly share code, notes, and snippets.

@michalzubkowicz
Forked from monteiro/Application.java
Created November 5, 2013 10:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michalzubkowicz/7316931 to your computer and use it in GitHub Desktop.
Save michalzubkowicz/7316931 to your computer and use it in GitHub Desktop.
package controllers;
public class Application extends Controller {
// in cache during 1800 seconds (30 min)
@Cached(key = "pagingList", duration = 1800)
public static Result index(Integer page) {
String uuid = session("uuid");
if (uuid == null) {
uuid = java.util.UUID.randomUUID().toString();
session("uuid", uuid);
}
PagingList<Student> pagingList = null;
pagingList = (PagingList<Student>) Cache.get(uuid + "pagingList");
if (pagingList == null) {
// 15 records in each page
pagingList = Student.FIND.findPagingList(15);
}
// -1 because page starts on 0
Page<Student> currentPage = pagingList.getPage(page - 1);
// pagineList save in cache with unique uuid from session
Cache.set(uuid + "pagingList", pagingList);
List<Student> students = currentPage.getList();
Integer totalPageCount = pagingList.getTotalPageCount();
return ok(index.render(students, page, totalPageCount));
}
}
@(students: List[Student], currentPage: Integer, pageCount: Integer)
@main("Students Table") {
@pagination(currentPage, pageCount, 10)
@tableStudents(students)
}
@(currentPage: Integer, pageCount: Integer, blockPageSize: Integer)
<div class="pagination">
<ul>
@if(currentPage == 1) {
<li class="disabled"><span>&laquo;</span></li>
} else {
<li><a href="@routes.Application.index(currentPage - 1)">&laquo;</a></li>
}
@for(index <- utils.TemplateHelpers.createRange(currentPage, blockPageSize, pageCount)) {
@if(currentPage == index) {
<li class="disabled"><span>@index</span></li>
} else {
<li><a href="@routes.Application.index(index)">@index</a></li>
}
}
@if(currentPage == (pageCount-1)) {
<li class="disabled"><span>&raquo;</span></li>
} else {
<li><a href="@routes.Application.index(currentPage + 1)">&raquo;</a></li>
}
</ul>
</div>
# Where tables with pagination are presented
GET / controllers.Application.index(page:Integer)
# Map static resources from the /public folder to the /assets URL path
GET /assets/*file controllers.Assets.at(path="/public", file)
package models;
@Entity
public class Student extends Model {
@Id
public Long id;
public static Model.Finder<Long, Student> FIND =
new Model.Finder<Long, Student>(Long.class, Student.class);
public String name;
public String city;
public Integer age;
}
}
@(students: List[Student])
<table class="table table-striped table-hover">
<thead>
<tr>
<th>#</th>
<th>Name</th>
<th>Age</th>
<th>City</th>
</tr>
</thead>
<tbody>
@for((student,index) <- students.zipWithIndex) {
<tr>
<td>@student.id</td>
<td>@student.name</td>
<td>@student.age</td>
<td>@student.city</td>
</tr>
}
</tbody>
</table>
package utils
import play.api.templates._
object TemplateHelpers {
def createRange(page: Int, max: Int, pageCount: Int): Range = {
val middle: Int = max / 2
val minNumbering: Int = page - (middle)
val maxNumbering: Int = page + (middle - 1)
(minNumbering, maxNumbering) match {
case (minN, maxN) if maxN <= max && minN <= 0 => 1 to max
case (minN, maxN) if maxN > pageCount => minN until pageCount
case (minN, maxN) => minN to maxN
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment