ColdFusion Pagination Made Easy

<cfdump var="#listLen(arguments.urlString,"&")#" />

<cfabort />

<cfset local.multiUrlParamsReplace = "&" />

</cfif>

<!--- get the number of pages --->

<cfset local.paginationStruct["numberOfPages"] = Ceiling(arguments.totalCount/arguments.rowsPerPage) />

<!--- start count --->

<cfset local.paginationStruct["startCount"] = (((arguments.page-1) * arguments.rowsPerPage)+1) />

<!--- end count --->

<cfset local.paginationStruct["endCount"] = ((arguments.page-1) * arguments.rowsPerPage)+arguments.currentRecordCount />

<!--- total count (obvious) --->

<cfset local.paginationStruct["totalCount"] = arguments.totalCount / >

<!--- create the next and previous links --->

<cfif arguments.page LT local.paginationStruct["numberOfPages"]>

<cfset local.paginationStruct["nextLink"] = replace(arguments.urlString,"#local.multiUrlParamsReplace##arguments.pageVar#=#arguments.page#","")&"#arguments.pageVar#="&arguments.page+1 />

</cfif>

<!--- previous link --->

<cfif arguments.page LTE local.paginationStruct["numberOfPages"] AND arguments.page GT 1 >

<cfset local.paginationStruct["previousLink"] = replace(arguments.urlString,"#local.multiUrlParamsReplace##arguments.pageVar#=#arguments.page#","")&"#arguments.pageVar#="&arguments.page-1 />

</cfif>

<!--- handle the max before/after --->

<cfset local.maxPages = arguments.maxPagesBefore + arguments.maxPagesAfter + 1 />

<cfset local.startIndex = 1 />

<cfset local.endIndex = local.paginationStruct["numberOfPages"] />

<cfif local.paginationStruct["numberOfPages"] GT local.maxPages>

<cfset local.startIndex = arguments.page - arguments.maxPagesBefore />

<cfset local.endIndex = arguments.page + arguments.maxPagesAfter />

<cfif local.startIndex LT 1>

<cfset local.startIndex = 1 />

<cfset local.endIndex = (local.startIndex + local.maxPages) - 1 />

</cfif>

<cfif local.endIndex GT local.paginationStruct["numberOfPages"]>

<cfset local.startIndex = local.paginationStruct["numberOfPages"] - local.maxPages />

<cfset local.endIndex = local.paginationStruct["numberOfPages"] />

</cfif>

</cfif>

<!--- loop through to create the array of objects only if the endIndex is GT 1 --->

<cfif local.endIndex GT 1>

<cfset local.displayLinks = ArrayNew(1) />

<cfloop from="#local.startIndex#" to="#local.endIndex#" index="local.i">

<cfset local.pageObj = StructNew() />

<cfset local.pageObj["pageNumber"] = local.i />

<cfset local.pageObj["pageLink"] = "?"&replace(arguments.urlString,"#local.multiUrlParamsReplace##arguments.pageVar#=#arguments.page#","")&"#arguments.pageVar#="&local.i />

<cfif arguments.page EQ local.i>

<cfset local.pageObj["isCurrentPage"] = true />

<cfelse>

<cfset local.pageObj["isCurrentPage"] = false />

</cfif>

<cfset ArrayAppend(local.displayLinks,local.pageObj) />

</cfloop>

<cfset local.paginationStruct["displayLinks"] = local.displayLinks />

</cfif>

<cfreturn local.paginationStruct />

</cffunction>

</cfcomponent>

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.

<!--- instantiate the pagination object --->

<cfset pagination = new paginate() />

<cfset paginate = pagination.createPagination(argumentCollection = paginationArgs) />

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:

<cfparam name="attributes.paginationObj" default="" />

<cfif !IsStruct(attributes.paginationObj)>

Page 2 of 3 | Previous page | Next page