Ask a Jedi: Paging within cfsearch?

This post is more than 2 years old.

Peter asks:

Whats the BEST way to page results from cfsearch?

I think my readers are being a bit pushy lately on "best" - I'm feeling the pressure. ;) Luckily this one is easy.

The cfsearch tag allows for both a startrow and maxRows value. So to paginate, you would simply pass in startrow and set maxRows to startrow plus some arbitrary number for the number of results per page.

The only tricky bit is getting the total number of matches found. If you use the status attribute, like below:

<cfsearch collection="cfdocs" criteria="cf" name="results" startrow="5" maxrows="5" status="s">

You get a structure back that contains three keys: found, searched, and time. Found represents the total number of matches. You would want to use that for your paging.

p.s. Let me abuse my blog entry here and mention I added similar functionality to Seeker last night.

Raymond Camden's Picture

About Raymond Camden

Raymond is a senior developer evangelist for Adobe. He focuses on document services, JavaScript, 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

Archived Comments

Comment 1 by ryanTJ posted on 6/11/2008 at 8:51 PM

Pagination.cfc on RiaForge is a pretty nice pagination tool. You supply a query and the max records per page and it handles start/end rows for you. You still display your results but pagination.cfc creates the prev/next and page numbers for you.

Comment 2 by Andy Sandefer posted on 6/17/2008 at 7:35 AM

<cfreturn QueryConvertForGrid(qrySearchResults,, ARGUMENTS.pageSize)>

Comment 3 by Raymond Camden posted on 6/17/2008 at 12:29 PM

@Andy - Yep, good one. That func was primarily built for Ajax, but you can use it outside of Ajax as well. Just remember it doesn't return a query though - if I remember right it's a struct with a query inside it.

Comment 4 by Andy Sandefer posted on 6/17/2008 at 6:56 PM

I'm a huge fan of the html grid and can't live without it! One of my clients has a ton of report files organized by department and I made a cfgrid on the left for department selection and a second (bound) cfgrid on the right which calls a method who in turn does a cfdirectory based on the selected department and then displays the cfdirectory's results to a grid so that I can display files in the right hand grid for whichever department is selected in the left hand grid. I took this a step further and added filter as you type functionality (kind of like what you did for RIA Forge Ray!) for both grids as well as Add, Edit and Delete using cfajaxproxy and cfwindow madness and they liked it.

This post has me thinking about cfsearching a collection based on indexed documents as a user types and dynamically refreshing a grid based on the collection's search results - this would be for my Convergence Product (I demo'd an earlier version of this during Best of Scorpio but you weren't there that day).

Anyway, this type of stuff runs way quick in Firefox and Safari but really awful in IE. I hope that IE8 is not so much of a turd at AJAX.

Comment 5 by futr_vision posted on 6/22/2009 at 7:23 PM

I'm quite getting how to paginate the results from cfsearch. is this applicable to results returned to a cfgrid? My understanding is that is not doable when a cfgrid is involved.

Comment 6 by Raymond Camden posted on 6/22/2009 at 11:30 PM

When you return data for cfgrid, it has to be in a special format. Something like a struct with a TOTAL and a DATA (query) key. I'm not 100% sure - but the docs are. If you use queryConvertForGrid, you don't have to worry about it.

Comment 7 by futr_vision posted on 6/23/2009 at 1:21 AM

I've got a cfsearch that does populate a cfgrid. I'm using 2 of the 4 custom fields to store that data. It outputs nicely to the cfgrid but pagination is non-existent despite defining pagesize. Are you saying I can use queryConvertForGrid with the cfsearch?

Please excuse me if that is a stupid question. I am very new to CF programming or any programming for that matter.

Comment 8 by Raymond Camden posted on 6/23/2009 at 1:28 AM

If you did query=... for the grid, it isn't an Ajax grid and pagination won't work. You need to load the data via a binding for it to be Ajax based and make use of paging.