ColdFusion in Context: Automatic Reports

It's easy, or at least straightforward, to create a columnar table by using HTML tags. However, suppose you need to create a plain text report formatted only with spaces. You could use the cfoutput tag and wrap each element in a function that pads it to a fixed length. However, there's an easier way: use CFTABLE. The CFTABLE tag lets you provide instructions stating how you want each column to be formatted. Although it can create HTML tables, its unique strength is its ability to build reports as preformatted text.

Data

To see an example of that strength, you'll need some data. Call the first column "Boss", the second "Title", and the third "Grp". Call the table "Org".

Northern Division Governor   Area 1 Governor    District              
Southern Division Governor   Area 10 Governor   District              
Southern Division Governor   Area 11 Governor   District              
Southern Division Governor   Area 12 Governor   District              
Northern Division Governor   Area 2 Governor    District              
Northern Division Governor   Area 3 Governor    District              
Northern Division Governor   Area 4 Governor    District              
Metro Division Governor      Area 5 Governor    District              
Metro Division Governor      Area 6 Governor    District              
Metro Division Governor      Area 7 Governor    District              
Metro Division Governor      Area 8 Governor    District              
Metro Division Governor      Area 9 Governor    District              

Space-Formatted Table

This code produces a preformatted columnar report. The default column width is 20; make the first column larger than that to avoid TRUNCATING the output. (It won't wrap in text mode.)

<cfquery name="display" datasource="ic">
select * from org
</cfquery>
<cftable query="display">
<cfcol width="30" header="BOSS" text="#Boss#">
<cfcol header="TITLE" text="#Title#">
<cfcol header="GRP" text="#Grp#">
</cftable>

You'd think it would be easy to simplify this by writing cfcol tags with cfloop from the display.ColumnList variable. However, it won't work. The CFTABLE tag is perhaps the most primitive tag in the ColdFusion inventory. It does not permit any other tag to be used within it. So, you have to use it in the fashion shown above.

HTML Table

You can produce an HTML table with the addition of a single attribute...

<cfquery name="display" datasource="ic">
select * from org
</cfquery>
<cftable query="display" htmltable>
<cfcol header="BOSS" text="#Boss#">
<cfcol header="TITLE" text="#Title#">
<cfcol header="GRP" text="#Grp#">
</cftable>

Did you see the htmltable attribute? That's all it takes.

You'd think it would be easy to pick the kind of table you want by using a variable in place of "htmltable" and fill in the variable or leave it blank. This technique works other places in ColdFusion but not here. Again, this primitive tag will not recognize variables used in this manner.

Why not wrap the beginning tag in a cfif and simply pick a version with or without this attribute? This doesn't work either; ColdFusion sees the closing tag but claims not to see the opening tag.

Select a Table Format

Therefore, if you want to offer two different versions of a report selectable by a variable, you have to duplicate the whole report as shown here...

<cfparam name="url.mode" default="text">
<cfquery name="display" datasource="ic">
select * from org
</cfquery>
<cfif url.mode is "html">
  <cftable query="display" htmltable>
  <cfcol header="BOSS" text="#Boss#">
  <cfcol header="TITLE" text="#Title#">
  <cfcol header="GRP" text="#Grp#">
  </cftable>
<cfelse>
  <cftable query="display">
  <cfcol width="30" header="BOSS" text="#Boss#">
  <cfcol header="TITLE" text="#Title#">
  <cfcol header="GRP" text="#Grp#">
  </cftable>
</cfif>

Save this as table.cfm. Call it as table.cfm?mode=html to get a table made from HTML tags. If you call it as table.cfm with no parameters or some other parameter, it produces a report formatted with spaces, using a non-proportional font. View the source to see the difference under the hood. Test my claims regarding its incompatibility with variables and other tags.

If you use this tag to make an HTML table, it has other attributes you can specify to dress it up a bit. However, I think it's most exciting to use in creating plain text reports on the fly. =Marty=