Pager support for Play2 application.
Scala 2.11.x & 2.12.x Play 2.5.x & 2.6.x
libraryDependencies += "jp.t2v" %% "play2-pager" % "0.2.0"
libraryDependencies += "jp.t2v" %% "play2-pager-scalikejdbc" % "0.2.0" // optional. it is useful when you use scalikejdbc.
-
add imports into
templateImports
androutesImport
in your Play Project.sbt
.lazy val sample = (project in file("sample")). enablePlugins(PlayScala). settings( // ...snip templateImports ++= Seq( "jp.t2v.lab.play2.pager._" ), routesImport ++= Seq( "jp.t2v.lab.play2.pager.Pager", "jp.t2v.lab.play2.pager.Bindables._" ), )
-
define implicit
Sortable
value of your entities.default
is default sorting key and direction.acceptableKeys
is sortable keys of the entity.
package models.account import scalikejdbc._ import org.joda.time.{DateTime, LocalDate} import jp.t2v.lab.play2.pager.{OrderType, Sortable} case class Account(id: Int, name: Name, email: EMail, birthday: LocalDate, createdAt: DateTime) object Account extends SQLSyntaxSupport[Account] { def apply(s: SyntaxProvider[Account])(rs: WrappedResultSet): Account = autoConstruct(rs, s) implicit object sortable extends Sortable[Account] { val default: (String, OrderType) = ("id", OrderType.Descending) val acceptableKeys: Set[String] = Set("id", "name", "email", "birthday", "createdAt") } }
-
define controller that receive a
Pager
of your entity.def index(pager: Pager[Account]) = Action { Ok(views.html.index(accountService.findAll(pager))) }
-
define service that returns a
SearchResult
of your entity.package models.account import jp.t2v.lab.play2.pager.{SearchResult, Pager} class AccountService(implicit accountDao: AccountDao) { def findAll(pager: Pager[Account]): SearchResult[Account] = { SearchResult(pager, accountDao.countAll()) { pager => accountDao.findAll(pager.allSorters, pager.limit, pager.offset) } } }
-
define template that receive a
SearchResult
of your entity.You can use
@pagination
to render pagination.@(result: SearchResult[Account]) @main("Welcome to Play2 Pager") { ...snip @pagination(result, routes.Application.index) }
More details, see Sample App
- git clone
- cd play2-pager
- sbt "project sample" run
- browse
http://localhost:9000/
- Click
Apply this script now!
This library is released under the Apache Software License, version 2,
which should be included with the source in a file named LICENSE
.