ColdFusion ORM paging in FW/1 (Framework One)
Monday 05 December 2011
Firstly, in your Handler add maxresults and offset keys to the RC structure and give them some default values. Maxresults is the number of results to be displayed per page and offset is the number of rows to skip. For example, if the value of both maxresults and offset is 10, records 11 to 20 will be displayed. By default we'll want the value of offset to be 0, but you can set the value of maxresults to whatever you like.
<cfparam name="rc.maxresults" default="10" /> <cfparam name="rc.offset" default="0" />
Now you need to create your query and use the maxresults and offset keys as parameters to ensure you only return the records you need. You can create your query in one of three ways.
Method 1
<cfset rc.art = ORMExecuteQuery( "from Art", false, { maxresults=rc.maxresults, offset=rc.offset } ) />
Method 2
<cfset rc.art = EntityLoad( "Art", {}, "", { maxresults=rc.maxresults, offset=rc.offset } ) />
Method 3
<cfset var ormoptions = { maxresults=rc.maxresults, offset=rc.offset } />
<cfquery name="rc.art" dbtype="hql" ormoptions="#ormoptions#"> from Art</cfquery>
Next, get the total count of all your records. We'll need this later.
<cfset rc.artcount = ORMExecuteQuery( "select count( * ) from Art", true ) />
Now in your View we'll add the previous and next page links.
<!--- display the art (but styled nicely) ---> <cfdump var="#rc.art#" /> <!--- if the offset value is greater than zero we need to display a link to the previous page of results ---> <cfif rc.offset> <!--- build a url to the current page, but set the offset value to the current offset value less the value of maxresults ---> <a href="#buildURL( action='portfolio:', querystring='offset=#rc.offset-rc.maxresults#' )#">« Previous</a> </cfif> <!--- if the total count of all your records is greater than the sum of the maxresults and offset values you'll need to display a link to the next page of results ---> <cfif rc.maxresults + rc.offset lt rc.artcount> <!--- build a url to the current page, but set the offset value to the current offset value plus the value of maxresults ---> <a href="#buildURL( action='portfolio:', querystring='offset=#rc.offset+rc.maxresults#' )#">Next »</a> </cfif>
And that's it!
Tags
ColdFusion (13) FW/1 (2)Share
Comments
Be the first to add a comment!