How to use Liftweb’s built-in functionality for Sorting and Pagination using Scala

In this blog , we will have a basic idea that how to use LiftWeb’s built -in functionality for Sorting and Pagination .
Lift provides SortedPaginatorSnippet for sorting of results and PaginatorSnippet for paging of results .
Suppose you are displaying an item list on web and you want to sort and paginate that list then you have to implement both snippets SortedPaginatorSnippet and PaginatorSnippet in your snippet class file .

class Thingstodosnips extends SortedPaginatorSnippet[ThingsToDo, Date] with PaginatorSnippet[ThingsToDo] {

In SortedPaginatorSnippet , you have to pass type of the elements and type of the fields , which we are going to sort . In PaginatorSnippet , you have to pass type of elements .
After implementing both snippets , now you have to implement header method to specify column , which we are going to sort .

 def headers: List[(String, java.util.Date)] = List(("duedate",

If you are not using sorting , you do not need to implement header method .
Now you have to implement sortOrder method to deal with the database . Here we are using MongoDB , so you have to define sortOrder method in such a way :

def sortOrder = S.param("asc") match {
    case e: EmptyBox => -1
    case b: Box[String] => if (b.get.equalsIgnoreCase("true")) 1 else -1

For Pagination , you have to override count , itemPerPage and page properties .

count – number of all items you have (here all Measurements)
page – represents current slice
itemPerPage – Number of Items per page

override def count = ThingsToDo.findAllNotes(
  override def itemsPerPage = 5
  override def page = ThingsToDo.findAllNotes(
    (curPage * itemsPerPage))

Finally add markup in your HTML :

For Sorting :

<th width="20%" class="lift:thingstodosnips.paginate"><sort:duedate>Due Date</sort:duedate></th>

For Pagination :

	   <nav:first></nav:first> | <nav:prev></nav:prev> | <nav:allpages></nav:allpages> |   <nav:next>     </nav:next> | <nav:last></nav:last>

You can found source code on the Knoldus GitHub account here : Login Template using Liftweb and Scala .

About Ayush Mishra

Ayush is the Sr. Software Consultant @ Knoldus Software LLP. In his 5 years of experience he has become developer with proven experience in architecting and developing web applications. Ayush has a Masters in Computer Application from U.P. Technical University, Ayush is a strong-willed and self-motivated professional who takes deep care in adhering to quality norms within projects. He is capable of managing challenging projects with remarkable deadline sensitivity without compromising code quality. .
This entry was posted in AJAX, Java, LiftWeb, Scala, Web. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s