So we have our first Ask an Emperor question from our reading audience. Lets see how it goes...
I need a simple to use component for query browsing in cf. (you know, the whole next 10, prev 10 thing). Any ideas?You have heard before I am sure that there is more than one way to skin a cat, and the saying, as disgusting as it may be, rings true here as well. What you are talking about I believe is called query pagination, where you show a certain amount of results on each page. It really isn't difficult no matter how you do it, and its fairly simple to give your users some preferences to set as well.
First off, the two things that make this really possible is a couple attributes on some of our favorite tags. With the cfoutput tag you have an attribute called startrow and another called maxrows. These can be used to tell the cfoutput, here is the recordset I want you to use, and I want you to start at this specific record, and show the maxrows number of records. I prefer though in this case to use the cfloop tag though, because it provides you with a startrow attribute as well as an endrow attribute, which makes things a little easier, in my opinion. So with those attributes, all thats left is to figure out what the numbers are. You are going to need five variables. startRow, endRow, nextStartRow, lastStartRow and rowsPerPage. You will want to param these variables in case they dont exist. All you really need to pass from one page to another though is the startRow variable, everything else can be figured out, if you store the rowsPerPage in an application or session wide variable. Then to figure things out, its just a little logic at the top of the page.
All thats left is for you to make your links for next and back using the nextStartRow and lastStartRow variables and to plug in the startRow and endRow into your cfloop tag. Now I want to leave you with this. I definately do not suggest re-running your query on every page to plug into your cfloop! So I would do one of two things, either cache the query using the cachedwithin attribute (remember you cannot use queryparam though) or store the recordset in the session scope for use on every page. Hope that helps.