Pagination in lift?

11 views
Skip to first unread message

Naftoli Gugenhem

unread,
Jun 28, 2009, 3:44:27 PM6/28/09
to lif...@googlegroups.com
Does lift have built in support for pagination -- breaking up a query and continuing it across multiple pages, and clicking the headers to sort?
If not how hard is it?

Timothy Perrett

unread,
Jun 28, 2009, 4:06:48 PM6/28/09
to Lift
You don't mention if your using JPA or Mapper - if your using mapper
then the answer is "no" we don't have any built in support but its not
to difficult to display limited lists. You could grab all the results
then do some manipulation on the list in memory if you only have a
small dataset, alternatively you could craft your own queries using
limiting / offsetting.

Alternatively, if your using JPA, you can use the stuff built into
jpa; more info here: http://is.gd/1hfCg

Cheers, Tim

Naftoli Gugenhem

unread,
Jun 28, 2009, 5:30:38 PM6/28/09
to tim...@getintheloop.eu, lif...@googlegroups.com
Thanks.
(I was asking about the view end more than the model side, but I'm using Mapper.)
So can you specify limits and offset without resorting to native queries? If so, how? And if not, what would it take to write cross-database lift support?

-------------------------------------

Marc Boschma

unread,
Jun 28, 2009, 5:33:48 PM6/28/09
to lif...@googlegroups.com
See the Lift book (http://tinyurl.com/mta3h5), section 6.1.10 & 6.1.8
which discusses pagination with Mapper...

Marc

Kris Nuttycombe

unread,
Jun 28, 2009, 9:14:18 PM6/28/09
to lif...@googlegroups.com
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

David Pollak

unread,
Jun 29, 2009, 2:09:56 PM6/29/09
to lif...@googlegroups.com
On Sun, Jun 28, 2009 at 1:06 PM, Timothy Perrett <tim...@getintheloop.eu> wrote:

You don't mention if your using JPA or Mapper - if your using mapper
then the answer is "no" we don't have any built in support but its not
to difficult to display limited lists. You could grab all the results
then do some manipulation on the list in memory if you only have a
small dataset, alternatively you could craft your own queries using
limiting / offsetting.

Tim,

Selecting a subset of stuff with Mapper is pretty simple:

Users.findAll(OrderBy(User.id, Ascending), StartAt(50), MaxRows(10))

 


Alternatively, if your using JPA, you can use the stuff built into
jpa; more info here: http://is.gd/1hfCg

Cheers, Tim

On Jun 28, 8:44 pm, Naftoli Gugenhem <naftoli...@gmail.com> wrote:
> Does lift have built in support for pagination -- breaking up a query and continuing it across multiple pages, and clicking the headers to sort?
> If not how hard is it?




--
Lift, the simply functional web framework http://liftweb.net
Beginning Scala http://www.apress.com/book/view/1430219890
Follow me: http://twitter.com/dpp
Git some: http://github.com/dpp

Timothy Perrett

unread,
Jun 29, 2009, 3:31:23 PM6/29/09
to Lift

> Tim,
>
> Selecting a subset of stuff with Mapper is pretty simple:
>
> Users.findAll(OrderBy(User.id, Ascending), StartAt(50), MaxRows(10))

Oh wow, really didnt know that was possible... i'll shut my jpa-using
mouth in future ;-) hehe.

Cheers, Tim
Reply all
Reply to author
Forward
0 new messages