Previously, I tried to build a table structure using just nested ColdFusion custom tags. In that scenario, the base Table tag stored the rows in local structures and the Row tag stored the cells in a local structure. I didn't quite like the way the code was working. For this attempt, I am aiming for the same outcome, however, instead of storing the data directly, the base Table tag will store a ColdFusion component. This CFC will be responsible for maintain an internal data structure that represents the rows and cells of the table.
The idea here is that the ColdFusion custom tags act as data collectors - they merely execute and figure out what data they have. The ColdFusion component, Data.cfc, is responsible for accepting data, knowing which row or cell to apply it to, and maintaining an internal data structure.
The Data.cfc ColdFusion component is quite simple. It keeps an array of arrays and has a method for adding a row, adding a cell, and getting the rows array:
Launch code in new window » Download code as text file »
Our index page (index.cfm) has not changed at all. It defines all the table, rows, and cell custom tags:
Launch code in new window » Download code as text file »
What has changed is our custom tags. Here, the table tag (table.cfm) now instantiates the Data.cfc when it starts to execute:
Launch code in new window » Download code as text file »
As you can see, it creates the Data component. Then, when the tag is done executing, instead of looping over its own Rows array, it grabs the Rows array out of the data component and renders the table.
The row and cell ColdFusion custom tags have changed slightly, but not all that much. They both still get pointers to the parent tags, but instead of adding themselves to the parent's data array, they are now both invoking methods of the Data ColdFusion component.
Here is the row tag (row.cfm):
Launch code in new window » Download code as text file »
Here is the cell tag (cell.cfm):
Launch code in new window » Download code as text file »
Running the above, we get the same output as we did in our previous example:
| | | | ||
| | ![]() | | ||
| | | |
I like this method better than the previous. There was something very cumbersome about having to maintain a table data structure in the parent tag and in the row tag; it just felt very hacky. Having the table, row, and cell tags all call this centralized Data ColdFusion component just feels like a slightly more elegant solution.
Download Code Snippet ZIP File
Comments (2) | Post Comment | Ask Ben | Permalink | Other Searches | Print Page
why?
Posted by jon on Jul 13, 2007 at 5:38 PM
To learn the ins and outs of working with ColdFusion custom tags. It was an exploration, not a suggestion as to how to make tables.
Posted by Ben Nadel on Jul 13, 2007 at 5:47 PM