ColdFusion in Context: Quick Pick

Suppose you want to include the criteria you used to pick records as part of the list of those records. Chances are, you used a single word or abbreviation to represent a few words describing your criteria. So how do you pass both those words and the search term to the action page based on checkboxes and select statements? One way is to let your users pick a list instead of a single value.

Create Some Data

To filter a synopsis of activities, you might want to distinguish between those for which yours is the Office of Primary Responsibility (OPR), the Office of Concurrent Responsibility (OPR), and activities for which office merely gets a courtesy copy (CC). Here's some sample data for a table named Activity which has a Title field and an Action field.

Title,Action
Fix Problem 1,OCR
News Article A,CC
News Article B,CC
Fix Problem 4,OCR
Fix Problem 5,OPR
News Article C,CC
Fix Problem 7,OCR
Fix Problem 8,OPR

Set Up the Form

Your user will need a form with which to select the kind of activity to see: myForm.cfm. As mentioned earlier, a form selection will need to pass two values: the search criteria and the human version of those criteria to display with the results of the search. An easy way to accomplish this is to assign both pieces of information as the value of a form option and separate them by a comma, making a list.

Select activities for which this office has...
<form name="getInfo" action="myList.cfm" method="post">
<select name="getPick">
<option value="OPR,has primary responsibility">Primary Responsibility
<option value="OCR,has shared responsibility">Shared Responsibility
<option value="CC,receives info only">Info Only
</select>
<input type="submit" name="doit" value="Get a List">
</form>

Set up the Action

Call the action page for the form getInfo.cfm. Begin it by assigning a default string with two empty, comma-separated values to the variable expected from the form page. Treat the string as a list and assign the second value in the string as the value for display. Then replace the string with its first value. (You could use a new variable name for the value to be passed to the query. This example merely points out that you don't have to.) The query is straightforward. The resulting list begins by displaying its criteria from a human point of view and then shows the data itself.

<cfparam name="form.getPick" default="'',''">
<cfset showPick=listGetAt(form.getPick,2)>
<cfset form.getPick=listGetAt(form.getPick,1)>

<cfquery name="listActivity" datasource="context">
select title
from activity
where action = '#form.getPick#'
</cfquery>

Activities for which this office <cfoutput>#showPick#</cfoutput>...<br>
<cfoutput query="listActivity">#title#<br>
</cfoutput>

Put it Together

To demonstrate, start from myForm.cfm, select a type of activity, and see the result produced by myList.cfm. This technique doubles the functionality of a simple quick pick. =Marty=