gakuzzzz / play2-pager   0.2.0

GitHub

pager support for Play2 application

Scala versions: 2.12 2.11 2.10

Play2-Pager Build Status

Pager support for Play2 application.

sample app screenshot

Target

Scala 2.11.x & 2.12.x Play 2.5.x & 2.6.x

Setup

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.

How to use

  1. add imports into templateImports and routesImport 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._"
        ),
      )
  2. 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")
      }
    
    }
  3. define controller that receive a Pager of your entity.

      def index(pager: Pager[Account]) = Action {
        Ok(views.html.index(accountService.findAll(pager)))
      }
  4. 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)
        }
      }
    
    }
  5. 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

Sample App

  1. git clone
  2. cd play2-pager
  3. sbt "project sample" run
  4. browse http://localhost:9000/
  5. Click Apply this script now!

License

This library is released under the Apache Software License, version 2, which should be included with the source in a file named LICENSE.