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.
Archived Comments
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.
<cfreturn QueryConvertForGrid(qrySearchResults, ARGUMENTS.page, ARGUMENTS.pageSize)>
@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.
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.
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.
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.
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.
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.