-
-
Save michalzubkowicz/7316931 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@(students: List[Student], currentPage: Integer, pageCount: Integer) | |
@main("Students Table") { | |
@pagination(currentPage, pageCount, 10) | |
@tableStudents(students) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@(currentPage: Integer, pageCount: Integer, blockPageSize: Integer) | |
<div class="pagination"> | |
<ul> | |
@if(currentPage == 1) { | |
<li class="disabled"><span>«</span></li> | |
} else { | |
<li><a href="@routes.Application.index(currentPage - 1)">«</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>»</span></li> | |
} else { | |
<li><a href="@routes.Application.index(currentPage + 1)">»</a></li> | |
} | |
</ul> | |
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@(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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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