Paul Robertson's words, punctuated

Thoughts on development, user-centered design, code, etc. by Paul Robertson

How to: Select all checkboxes in ColdFusion Flash Forms

Part 1 of My first foray into ColdFusion Flash Forms.

I actually came up with two different approaches to this problem, because we figured out two different ways to dynamically create form fields in Flash forms. Conceptually they are similar, of course; you have a button which calls some ActionScript code that loops through all the checkboxes and sets their “selected” property to true. Unfortunately the implementation details are pretty different.

Approach 1: Using cfformgroup type="repeater" (Flex creates the checkboxes)

Source Code

In this approach, the checkbox controls are dynamically created by the SWF file. The Flex code that creates them conveniently creates an array whose elements are the dynamically created checkbox controls, so we just loop through that array and set the “selected” property of each element to true (or false for “deselect”). The array variable you use has the same name as the name you give to the cfinput type="checkbox" in your ColdFusion code. Your ColdFusion code to create the repeating checkboxes looks something like this:

.
.
.
<cfformgroup type="repeater" query="q1">
    <cfinput type="Checkbox" name="chk" Label="{q1.currentItem.firstname}">
</cfformgroup>
.
.
.

Flex automatically creates an array named “chk” which you can refer to in your ActionScript code, whose elements are the Checkbox controls.

Approach 2: Use cfloop to create multiple checkboxes in the form

Source Code

In this approach, the checkbox controls are created in a cfloop that runs on the server. To create the “select all” functionality we just loop through our query and dynamically create the ActionScript that will be used by the “select all” button like this:

<cfsavecontent variable="selectAllClick">
    <cfloop query="q1">
        <cfoutput>boxMBAa_#q1.id#.selected = true;</cfoutput>
    </cfloop>
</cfsavecontent>

Note: Unfortunately the only ColdFusion servers I have access to belong to my employer, so I can only post source code and not working examples on my personal site. Sorry!

Comparing the two approaches

Approach 1 advantage

The repeating controls are actually generated in ActionScript in the SWF file, not on the server, so when the data changes the SWF file doesn’t need to be recreated by the server. See the article “Creating ‘dynamic’ form fields in ColdFusion Flash Forms - how and why” for more details.

Approach 2 advantage

Each checkbox control is submitted as a separate form field (name/value pair); you can define the names given to the form fields. See the article “Getting meaningful data from dynamically generated fields in ColdFusion Flash Forms” for more details.

Approach 1 disadvantage

The checkbox values are submitted as two lists, one containing true/false values indicating whether a checkbox was checked or not, and the other containing a list of “id” values which correspond to the true/false values. See the article “Getting meaningful data from dynamically generated fields in ColdFusion Flash Forms” for more details.

Approach 2 disadvantage

The repeating controls are generated by the ColdFusion server before creating the MXML and subsequent SWF file. When the data changes (and maybe every time the page is requested) the server must regenerate the MXML and recompile the SWF. See the article “Creating ‘dynamic’ form fields in ColdFusion Flash Forms - how and why” for more details.

Next: Advice for building and debugging ColdFusion Flash Forms, from one beginner to another

Comments