Just to thank everyone on this mailing list (and especially Justin Kidman who helped
me with this) I present for your dissecting pleasure: CF_OUTPUTPG.cfm
I wrote this module both because I needed it and as an exercise in writing my first
custom tag.
It works the same as CFOUTPUT QUERY="queryname" with the added feature that it PAGES
long results one block per web page. It also returns all the numbers you need to put a
NEXT and PREV button on the bottom.
So, here it is, the source code and a sample test file that uses it: (forgive me if
Eudora messes up the indenting)
<!---
// Module outputpg.cfm
// Peter Theobald 8/10/2000
// [EMAIL PROTECTED]
// Easy output of query data in paged format
//
// Usage:
// <CF_OUTPUTPG query="queryname" {OPT} startrow=1 {OPT} maxrows=10 >
// ... CFML that uses #queryname.column# ...
// </CF_OUTPUTPG>
//
// Returns: outputpg.prevstart, outputpg.nextstart,
// outputpg.numpages, outputpg.thispage
--->
<cfif ThisTag.ExecutionMode is "end">
<cfparam name="attributes.startrow" default="1">
<cfparam name="attributes.maxrows" default="10">
<cfscript>
rc = evaluate("caller.#attributes.query#.recordcount");
// Why can't CF be zero based like any sane language?
numpages = (rc-1)\attributes.maxrows +1;
thispage = (attributes.startrow-1)\attributes.maxrows +1;
prevstart = ((attributes.startrow-2)\attributes.maxrows)
* attributes.maxrows +1;
if (prevstart LT 0) prevstart = 1;
nextstart = ((attributes.startrow+attributes.maxrows)\attributes.maxrows)
* attributes.maxrows +1;
if (nextstart GT rc)
nextstart = attributes.startrow;
// Make these available to the caller:
caller.outputpg.numpages = numpages;
caller.outputpg.thispage = thispage;
caller.outputpg.prevstart = prevstart;
caller.outputpg.nextstart = nextstart;
// put query in local scope to evaluate tag body
scopeexp = "#attributes.query# = caller.#attributes.query#";
evaluate(scopeexp);
</cfscript>
<cfoutput query="caller.#attributes.query#" startrow=#attributes.startrow#
maxrows=#attributes.maxrows#>
#Evaluate("""#ThisTag.GeneratedContent#""")#
</cfoutput>
<cfset ThisTag.GeneratedContent = "">
</cfif>
<!--- --- --- --- END OF FILE --- --- --- --->
<!---
// Testpg.cfm
// Peter Theobald 8/10/2000
// [EMAIL PROTECTED]
//
// This file is a simple example of using the CF_OUTPUTPG module
// Make sure outputpg.cfm is in the same directory or in your CustomTags directory
--->
<!---
// replace this with your own favorite query
// make it 3 columns if you want to keep
// all the formatting the same as whats already below.
--->
<cfquery name="q_users" datasource="#MyDSN#">
select UserName, UserType, Email
from Users
</cfquery>
<html><body>
This is a test of custom tag CF_OUTPUTPG.<p>
<cfparam name="url.startrow" default="1">
<table border=1 width=50%><tr><td>User</td><td>Type</td><td>Email</td></tr>
<cf_outputpg query="q_users" startrow="#url.startrow#">
<tr><td>#UserName#</td><td>#UserType#</td><td>#Email#</td></tr>
</cf_outputpg>
</table>
<table border=0 width=50%>
<tr><cfoutput>
<td><a href="testpg.cfm?startrow=#outputpg.prevstart#">PREV PAGE</a></td>
<td align=center>Page #outputpg.thispage# of #outputpg.numpages#</td>
<td align=right><a href="testpg.cfm?startrow=#outputpg.nextstart#">NEXT PAGE</a></td>
</cfoutput></tr></table>
</body></html>
<!--- --- --- --- END OF FILE --- --- --- --->
Further enhancements:
o Add "calculate only" mode. If the 'query' argument is missing and the
'numrecords' argument is present
simply calculate all the numbers and return them. This will be useful if the
user wants to put the page number
or NEXT and PREV buttons *before* the actual results display.
o Accept 'group' and 'groupcasesensitive' arguments and pass them through to the
CFOUTPUT
---------------------------------------------------------------------------
Peter Theobald, Chief Technology Officer
LiquidStreaming http://www.liquidstreaming.com
[EMAIL PROTECTED]
Phone 1.212.545.1232 Fax 1.212.679.8032
------------------------------------------------------------------------------
Archives: http://www.mail-archive.com/[email protected]/
To Unsubscribe visit
http://www.houseoffusion.com/index.cfm?sidebar=lists&body=lists/cf_talk or send a
message to [EMAIL PROTECTED] with 'unsubscribe' in the body.