![]() ![]() ![]() |
Presenting query result sets using simple HTML formatting is usually adequate if the number of records returned is small. However, you might need a more compact and structured display of query results. Because the CFOUTPUT tag can include HTML code, you can use standard HTML table tags to build a table dynamically.
CFML also includes a pair of table tags, CFTABLE and CFCOL, that work together to present query results in an useful tabular format. This format uses the HTML <PRE> tag to precisely control the width and alignment of the columns displayed. The result is a clear, concise rendering of your query results.
This example uses the CFTABLE and CFCOL tags to present output:
<CFTABLE QUERY="Messages" MAXROWS=10> <CFCOL HEADER="Subject" WIDTH=25 TEXT="<I>#Subject#</I>"> <CFCOL HEADER="User Name" WIDTH=15 TEXT="#UserName#"> <CFCOL HEADER="Date" WIDTH=15 ALIGN=RIGHT TEXT="#DateFormat(DateSubmitted)#"> </CFTABLE>
This code creates a table with three columns labeled "Subject," "User Name," and "Date." These labels appear only if you specify at least one of the HEADER attributes. The table draws its data from the CFQUERY named "Messages" and shows no more than 10 rows. The columns display according to the HTML tags and dynamic parameters contained in their associated TEXT attribute.
Since ColdFusion uses the double-quotes (") and the pound sign (#) to delimit the TEXT values of the CFCOL attribute, both require special syntax. To specify a single double-quote, use two double-quotes (""). To specify a single pound sign, use two pound signs (##).
For example, to present a list of documents and provide a hyperlink to each document, you must include a hypertext anchor tag (which uses double-quotes to delimit its HREF attribute) within the TEXT attribute. To accomplish this, use the syntax:
TEXT="<A HREF=""/documents/#DocFile#"">#DocName#</A>"
When ColdFusion processes the code, it replaces the two double-quote pairs surrounding the HREF attribute with a solitary double-quote pair.
Even though the preformatted tables produced by the CFTABLE tag are effective, HTML tables are far more elegant and flexible. To make this transition easier, another CFTABLE attribute called HTMLTABLE is available. This attribute renders a CFTABLE as an HTML table rather than as a preformatted table.
Using the HTMLTABLE attribute changes the interpretation and applicability of some CFTABLE and CFCOL attributes:
Displaying result sets using HTML tables does not require the use of the CFTABLE tag with the HTMLTABLE attribute. You can also use the CFOUTPUT tag to construct HTML tables. For maximum control over the appearance of your tables, the CFOUTPUT tag is clearly a superior approach.
The most commonly used format for displaying the results of a search is a table such as a CFTABLE or other highly compact listing. This type of listing is excellent for the purpose of quickly browsing records but is often limited in the amount of information conveyed.
The solution to this problem is to display records in a compact form and then create a hypertext link from each record to another page that provides more detailed information. This type of master-detail approach is explained, including how to implement this functionality from within both CFOUTPUT and CFTABLE tags.
Creating a link to detail records involves the steps below, assuming you already have an application page file that displays a summary list of records.
Suppose you want to display a list of companies and let the user click on a company's name to get more detailed information on the company. Further assume that each company is represented in the database by a unique Company_ID and that you have an application page file called compinfo.cfm
that can display information on a company given a passed Company_ID.
In this case, your CFOUTPUT section would look similar to the example below:
<CFOUTPUT Query="CompanySearch"> <A HREF="compinfo.cfm?Company_ID=#Company_ID#"> #CompName#</A><BR> #ContactPerson# (#ContactPhone#) <BR> </CFOUTPUT>
The CompanyName field displays as a hypertext link, which invokes the application page file compinfo.cfm
with the Company_ID as a URL argument. This application page then displays the appropriate detail information for the company.
Use this technique whenever you want to implement dynamic display of detail records. The general form of the technique is as follows (note once again that the HREF should be contained on a single line in your application pages):
<A HREF="TemplateFile?Record_ID=#Record_ID_Data#">#RecordName#</A>
Here, TemplateFile is the name of the application page that implements the record detail view. This application page uses the Record_ID_Data (passed as the URL parameter Record_ID) to determine which record to display. The user can click the text RecordName in the main listing to invoke the application page.
You can also use this technique for embedding record detail links inside the TEXT attribute of CFCOL tags. Because the TEXT attribute is delimited by quotes and the HREF attribute of the anchor is also delimited by quotes, be sure that you escape the HREF quotes properly.
The general form of the technique is modified as follows to work properly inside a TEXT attribute:
<A HREF=""TemplateFile?Record_ID=#Record_ID_Data#"">#RecordName#</A>
Note that there are now two quotes surrounding the HREF attribute of the anchor. If the earlier example were modified to display inside a TEXT attribute, it would look like this:
TEXT="<A HREF=""compinfo.cfm?Company_ID=#Company_ID#"">#CompName#</A>"
Once again, be sure you keep the entire TEXT attribute on a single line.
You can create simple drill down search applications that use the techniques explained in this section with one of the ColdFusion Web Application Wizards. To run a wizard, click the Web Application Wizards icon in the ColdFusion program group.
![]() ![]() ![]() |
AllaireDoc@allaire.com
Copyright © 1998, Allaire Corporation. All rights reserved.