November 28, 2014
Hot Topics:

Implementing Search Result Pagination in a Web Application

  • August 24, 2007
  • By Vlad Kofman
  • Send Email »
  • More Articles »
private String getSearchLinks(String query, int start, int total) {
// assuming that initial page = 1, total = 0, and start is 0

   String result = "";
   int start = 0;

   if (total == 0) { return "";    // no links }

   Int page_size = 50;    // number of rows per page
   //page window - number of visible pagers per page
   Int window = 10;


   int pages = ceil(total / page_size );
   result = "Pages:";

   int current_page = (start / page_size ) + 1;
   //numeric value of current page ex. if start is 51 : 51/50 =
   // 1 + 1 = 2

   Int left_link_count = ((current_page - 1) > (window / 2))
      ? (window / 2 - 1) : (current_page - 1);
   Int pageNo = current_page - left_link_count;

   if (pageNo > 1) {    // show first page and if there are more
                           // links on the left
      result += "1 .. ";
      result += "«";
   }

   for (int i = 0; i < window-1; i++) {
      if (pageNo > pages) {
         break;
      }
      else if (pageNo == current_page) {
         result += "" + pageNo + "";
      }
      else {
         result += "pageNo";
      }
      pageNo++;
   }    // end for

   if ((pageNo - 1) < pages) {
      result += "»";
   }

   result += "
Showing"+((start > total)?total+1:start+1)+ " - "+(((start + 50)>total)?total:(start + 50))+" of Total:"+total; return result; }

This logic does not care how the viewable portion of the result set is generated. Whether it is on the database side or on the application server's side, all this algorithm needs is a "total" number of results (that can be pre-cached after the first retrieval), and the indicator ("start") containing which row number was the first on the last page user was looking at. This algorithm also shows the first page link if the user is on a page beyond the initial page window (for example, page 20), and correctly accounts for the result sets with a number of rows not enough for 10 pages. (for example, only 5 pages)

The main "for loop" generates the links and correctly computes the "start" parameter for the next page. The Query string and Total are always the same values.

Conclusion

In this article, I have discussed different methodologies in implementing pagination links in a web application. I have covered the database-driven approach, the middle-tier approach, and some third-party solutions. I have also detailed the actual algorithm for the page links generation. Ultimately, which solution you chose will depend on the size of the dataset, technology at hand, size of the site, and the budget for the project.

References

About the Author

Vlad Kofman is working on enterprise-scale projects for the major Wall Street firms. He has also worked on defense contracts for the U.S. government. His main interests are object-oriented programming methodologies, UI, and design patterns.





Page 3 of 3



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Sitemap | Contact Us

Rocket Fuel