ColdFusion and Pagination - Six Years Later

This post is more than 2 years old.

A little over six years ago I wrote a little blog post on how to do simple pagination in ColdFusion: ColdFusion and Pagination The post walked you through the simple step of iterating over a 'page' (or limited set) of data and providing links to navigate the data. For whatever reason, the post has gotten a few comments lately so I thought I'd post a quick update.

First off - Scott correctly pointed out that you should try your best to paginate in the database itself. I agreed - but to be fair - this example was meant for short sets of data that a user would actually peruse. I don't expect users to click "Next Page" over a few million records. That being said - you should try to offload as much as possible to the database and reduce the work your application server has to do.

Secondly - another reader, Ulises, asked if I could update my code to show the current page and the total number of pages. Both were rather easy. Let's look at the complete template and I'll explain how I added these features.

For the most part - this template is based on the code from the previous entry so I will not be covering every line. I did simplify the query a bit and used ColdFusion 10's simpler queryAddRow support.

The critical changes are these lines:

By using a simple bit of math, I can determine both what the total number of pages are as well as the current page. If I decide to change the size of a page later on the math will continue to work. And that's it. Not exactly rocket science, but the basic concept of paging over a set of data is still something folks like to see examples of, so I hope this is helpful! If you fancy seeing a demo, click the big obvious Demo button below...

Raymond Camden's Picture

About Raymond Camden

Raymond is a developer advocate for HERE Technologies. He focuses on JavaScript, serverless and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support. You can even buy me a coffee!

Lafayette, LA