If you're using JPA, pagination is pretty trivial. Here's what I use
in my app to provide it; the links stuff is primitive and needs
improvement but works.
import net.liftweb.util.Helpers._
import net.liftweb.http.SHtml._
import scala.xml._
class Paginator[A](val queryBase : String, val ordering :
Option[String], val params : Pair[String,Any]*) extends Paging[A] {
def count() : Long = {
val query = EM.createQuery[Long]("SELECT COUNT (o) " + queryBase)
for (p <- params) query.setParameter(p._1, p._2)
query.getSingleResult
}
/** 0 based page index. */
def getPage(page : Int, pageSize : Int) : Collection[A] = {
val query = EM.createQuery[A]("SELECT o " + queryBase +
ordering.getOrElse(""))
for (p <- params) query.setParameter(p._1, p._2)
query.setFirstResult(page * pageSize)
query.setMaxResults(pageSize)
query.getResultList
}
/**
* Display a pagination list for the order search.
*/
def pageLinks(maxRecords: Int, pageSetter: Int => Unit, loc:
String, xhtml : NodeSeq) : NodeSeq = {
val pageCount = (count / maxRecords).intValue + 1
(0 until pageCount).flatMap(i => bind("page", xhtml, "link" ->
link(loc, () => pageSetter(i), Text(i.toString))))
}
}
object Paginator {
def emptyPaginator[A] = new Paging[A]() {
def count = 0
def getPage(page : Int, pageSize: Int) : Collection[A] = Nil