Query Paging in Flex

I'm working on a top secret Flex project that will have a grid with the potential to display a large amount of records from a remoting call. Rather than risk the potential of poor performance down the road I decided that it would be best to cache the query in my component and then return incremental results via paging. I did some Googling for a pre-built solution but didn't find anything so I created my own function to handle the paging of the cached query. The code is below.

It's pretty straightforward, just pass in the query object, a start row and an end row. The function returns a struct containing a query starting at the start row argument and ending at the end row argument, a hasPreviousRows value (boolean) and hasMoreRows value (also boolean). Now I can easily page through the cached query in Flex by calling this function and dynamically enabling the 'next/previous' buttons.

<cffunction name="queryPage" access="public" output="false" returntype="struct">
   <cfargument name="theQuery" type="query" required="true">
   <cfargument name="sRow" type="numeric" required="true" hint="the start row of the query to return">
   <cfargument name="eRow" type="numeric" required="true" hint="the end row of the query to return">
   <cfset var returnStruct = structNew()>
   <cfset var increment = arguments.eRow - arguments.sRow +1>
   <cfset var returnQ = QueryNew(theQuery.columnList)>
   <cfset var row = 0>
   <cfset var col = "">
   <cfset returnStruct.hasPreviousRows = false>
   <cfset returnStruct.hasMoreRows = false>
   <cfif arguments.sRow gt arguments.eRow>
      <cfthrow type="queryPage Error" message="Start row must be less than or equal to end row.">
   </cfif>
   <cfif arguments.eRow gt theQuery.recordCount>
      <cfset arguments.eRow = theQuery.recordCount>
      <cfset increment = arguments.eRow - arguments.sRow +1>
   </cfif>
   <cfif sRow gt 1>
      <cfset returnStruct.hasPreviousRows = true>
   </cfif>
   <cfif eRow lt theQuery.recordCount>
      <cfset returnStruct.hasMoreRows = true>
   </cfif>
   <cfset queryAddRow(returnQ, increment)>
   <cfloop query="theQuery" startrow="#arguments.sRow#" endrow="#arguments.eRow#">
      <cfset row = row + 1>
      <cfloop from="1" to="#listLen(theQuery.columnList)#" index="col">
         <cfset querySetCell(returnQ, listGetAt(theQuery.columnList, col, ","), theQuery[listGetAt(theQuery.columnList, col, ",")][currentRow], row)>
      </cfloop>
   </cfloop>
   <cfset returnStruct.theQuery = returnQ>
   <cfreturn returnStruct />
</cffunction>

Comments
Thanks for blogging this.

I'm sure you're busy on the "top secret" project, but it would be helpful to see a Flex demo with the next/previous buttons and how you pass the query object from flex to the CFC function.

I would think there would be some internal Flex solution to paging using a datagrid that one could develop. It seems like extra work to have to keep going back to the CFC to get just the records you want to display in the datagrid. Especially since those records are already in the ArrayCollection (query) that Flex now knows about.

Once you have the complete query in flex, there should be some way using ActionScript to tell the datagrid what records (objects) out of the query (ArrayCollection) you want to display in the datagrid.

Perhaps you could design an ActionScript function that creates an ArrayCollection that only has a subset of the Objects (records) that you want to display in the datagrid and then make the dataGrid's dataprovider equal that ArrayCollection. When the user click on the Next button in Flex, you call a function that creates an ArrayCollection with the next 20 records and make that ArrayCollection the DataProvider for the datagrid.

Hmmm... this could be my next Flex Demo Application - Paging and the DataGrid Component.

I'm going to play with this further and see if I can figure out a Flex/ActionScript solution.
# Posted By Bruce | 11/28/06 4:54 PM
Hi guys,
You know that Flex Data Services can work with the Flex datagrid to do this automatically for recordsets of any size? The performance is just awsome, I saw it demoed at MAX where you could scroll to any place in a 20.000 recordset with instant results. Hardly any lag at all.

J
# Posted By Jensa | 11/28/06 5:27 PM
Bruce - check this post http://cfsilence.com/blog/client/index.cfm/2006/11...

Jensa - if only I had $20k to drop on FDS!
# Posted By todd sharp | 11/28/06 6:02 PM
Todd:

See: http://www.flashmagazine.com/1335.htm, where the author mentions using FLex Data Services to page data in a datagrid and also has some benchmark times for getting large amounts of data into a Flex app.

If you are building a large-scale app, then I think you should investigage further FDS. Does it really cost $20,000? It is possible that FDS will give you the speed and relability you need wihtout a bunch of custom-designed CF/ActionScript code.
# Posted By Bruce | 11/28/06 7:16 PM
You can also specify Flash.pageSize in your function. It has its cons too.

http://livedocs.macromedia.com/coldfusion/6.1/html...

Pim
# Posted By Philippe Maegerman | 11/29/06 4:38 AM
Reference my comment above about creating a demo of paging through a datagrid in Flex. You can read about and see the demo here:

http://www.brucephillips.name/blog/index.cfm/2006/...

It will not solve the challenge Todd is faced with, but may help others who need a simpler solution.

Bruce
# Posted By Bruce | 11/29/06 12:17 PM

Calendar

Sun Mon Tue Wed Thu Fri Sat
     12
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31       

Subscribe

Enter your email address to subscribe to this blog.

Tags

actionscript ajax blogging cfsnippets coldfusion flash forms flex funny stuff misc model-glue off topic personal project learn slidesix sql

Recent Comments

ColdFusion/Sharepoint Integration - Part 1 - Authenticating
todd sharp said: Jenn: Check your email. Todd [More]

ColdFusion/Sharepoint Integration - Part 1 - Authenticating
Jenn said: When does the next issue of FAQU come out? For that matter is it at all possible to get a preview o... [More]

A Major Milestone In My Marriage
Brian Meloche said: I converted my wife last year. That said, I find Firefox 3 a bit crashy. [More]

SlideSix Gets PDF Support
todd sharp said: Do you mean add numbering to the PDF output? If so, yeah, that would be easy. Just to be clear tho... [More]

SlideSix Gets PDF Support
salvatore fusto said: nice work Todd, but can you add slide numbering: 1/N, 2 of N and so long? it would be useful. regard... [More]

RSS


coldfusionbloggers

FullAsAGoog MXNA

Consumed By Feed-Squirrel.com