ColdFusion Pagination Made Easy

I know I haven’t posted in quite some time, but I’ve written a small method to help handle some of my day to day pagination challenges and I thought I’d share it. I’ve created a demo which uses MySQL, fake data, a ColdFusion customTag and the pagination method.

I imagine there’s quite a few developers that have already created something like this for themselves, so I apologize for being redundant if so. This is for the people who are constantly copying and pasting the same code over and over again throughout their applications. Not only that, but for anyone who really struggles to understand and implement pagination, this is great for you.

I won’t waste any of your time, so here’s a link to the demo if you want to skip the text and start testing it out already.

For the others that have stayed, I’m going to break down how to use it. Let’s start off with the a query. I’m using MySQL pagination, so it’s bit less involved than an RDBMS like Oracle. For anyone that needs assistance with implementing pagination with Oracle, please comment or send me an email and I’ll be happy to help out.





<!--- query for some data and cache the query --->

	SELECT first_name,last_name,email,zip, (select count(*) from member) as totalCount
	FROM member
	LIMIT ,




You’ll notice that I had to do a bit of work before hand to get get the appropriate “start” value for the SQL query. This is merely the product of “the amount of rows per page you want to show” * “your current page” – “the amount of rows per page you want to show”. So if you’re showing 30 records a page and you’re on page 3, you’re looking at: ((30 * 3) – 30) = 60. This is telling the query that you want to start retrieving records 60 rows into the result set and you’re limiting it by 30 rows. This would mean you’re seeing rows 61-90. I’m willing to bet this is overly thorough, but better too much than too little.

You’ll notice that I’ve got a structure with some values going around that query, I’ll explain now with the pagination function.



	
		
		
		
		
		
		
		
		
		
		
		
		
		
			
			
			
		
		
		
		
		
		
		
		
		
		

		
		 
			
		
		
		
			
		
		
		
		
	
		
		
		
		 
		
	
			
			
	
			
				
				
			
	
			
				
				
			
			

		
		
			
			
			
				
				
				
				
					
				
					
				
				
				
							
			
			
		
			
				
	

I’m not going to go into detail on what exactly everything does, but more of how you use it.

Remember that structure I was populating? We’re now going to instantiate the pagination class and then pass in that structure to the method.





When you dump the paginate variable, you should see something like this:

ColdFusion Pagination Made Easy example

Let’s jump into using this new structure returned from the createPagination method. It’s really just a matter of outputting the variables and then figuring out your display.

I went the customTag route when displaying my pagination because I’m going to show it on the top of the table and the bottom.

Let’s jump into using this new structure returned from the createPagination method. It’s really just a matter of outputting the variables and then figuring out your display.

I went the customTag route when displaying my pagination because I’m going to show it on the top of the table and the bottom.

Here’s what the customTag looks like:

« Previous #thePage.pageNumber# #thePage.pageNumber# Next »

The previous and next links are obvious, but in the middle you’ll see that I’m checking for the “displayLinks” key within the struct. This is an array of objects that contains your display of paging and current page. The links are already handled for you within the method and all you need to do is output the key value as shown with #thePage.pageNumber#. This is just to display the page numbers like Google, Bing or any other popular search engine. By default you have 3 numbers to your left and 3 numbers to your right, while having the active page selected in the middle. It’ll make more sense when you check out the demo, if you haven’t done so already.

You can overwrite just about everything for the method by changing an argument. You can have 4 numbers on each side just by passing in the following code before you’ve called the pagination method.





The additional methods are:



That sums it up for this ColdFusion pagination class. Please check out the demo and feel free to download the code and test it on your applications.

11 thoughts on “ColdFusion Pagination Made Easy”

  1. Hi Mike,
    Is there a way for you to help me when this is done in Oracle 11g. My dept. has just convert the db from Sybase to Oracle so I’m having trouble in the Oracle part. Thank you!

  2. Love it! Plan on using it for sure. One quick question. In your download, the index.cfm file shows that you instantiated the pagination object twice (once at the very top) and then again after you’ve setup the args. Just wondering if that’s needed.

  3. Just a note, on your example, if you add another url parameter to your url string and then try to go to the next page you will cause it to fail. I have modified the code on my end but just thought you should know.

    1. Hey Kelvin,
      I am a new to coldfusion.
      If you have solve this issue please help me
      I’m struggling a lot ..
      Thank you! in advance!

  4. Pagination is relatively straightforward with a linear list.

    Any comments on a forum style pagination? Where “replies” pose special handling?

  5. I have been using the pagination method here http://www.dopefly.com/projects/pagination/ However, when I tried to run it on a site with lots records, it slows the server down. Then I realized that it retrieve the whole sets of records then pagination. That is not efficient at all. Then I found yours which only retrieve for the page. Two questions:

    1. Is the argument at http://mysql.rjweb.org/doc.php/pagination valid?
    2. Is there any way that yours can be made to look like in their example http://www.dopefly.com/projects/pagination/examples/index.cfm?use=query&style=green&pagenumber=1 where you can see always see the first and last page?

    Thanks.

Leave a Reply

Your email address will not be published. Required fields are marked *