Some Tags Never Get Old

Word Count: 446

You heard me right I said tags not things. That is because today I came across another situation where a very popular custom tag was the right solution. If you have been developing for any amounts of time I am sure you have come across the related selects problem. What is that you ask? Two related selects is where the second select box is populated based on the users selection from the first. So how do you go about this without tricky Javascript? That is the easy part. A custom tag that has stood the test of time is Nate Weiss CF_TwoSelectsRelated, V1.0 custom tag still works like a charm. I have a list of most of the major colleges / universities in the United States and based on what state the user selects the second drop down will be populated with Colleges from that state.

The first thing you need to do is download the custom tag and you can it right from Adobe exchange. Unfortunately I have not found an easy way to link directly to the tag so you will need to search the exchange for two selects related. Once you have it downloaded place it right in the project directory or any directory in your custom tags path. The first code I need to get is a list of colleges. This query is basically going to be the college id, the name, and the state, the query will be ordered by the state. Here is my query.

<cffunction name="getCollegeList" access="public" output="false" returntype="query">
   <cfset var listColleges = "">
   
   <cfquery name="listColleges" datasource="#variables.dsn#">
   SELECT collegeId, name, state
   FROM college
   ORDER BY state,name
   </cfquery>
   
   <cfreturn listColleges>
</cffunction>

After we have a query containing the information needed I can now call the custom tag. We are just going to look at the code. If you want to find out what each attribute does the custom tag has some great documentation included.

<CF_TwoSelectsRelated
   QUERY="colleges"
   NAME1="state"
   NAME2="college"
   DISPLAY1="state"
   DISPLAY2="name"
   VALUE1="state"
   VALUE2="collegeId"
   EMPTYTEXT1="(choose a state)"
   EMPTYTEXT2="(now choose a college)"
   AUTOSELECTFIRST="No">

This is not groundbreaking code but I just wanted to emphasize that even with all the new technologies coming out, some stand the test of time. Having said that this problem could of been accomplished with Spry. I have used Spry before but not for this purpose. If I have a chance I may try and whip up an example.

Comments

#1 Posted By: Mike Posted On: 5/9/07 4:53 PM
hi dan, would really be interested in the spry example - have not had any experience with spry to be honest!
#2 Posted By: charlie griefer Posted On: 5/9/07 5:34 PM
there's already a spry demo for a two-related selects. john farrar put it together and charlie arehart links to it from his spry compendium.

http://carehart.org/spry/spry_resources.cfm#demos
http://carehart.org/spry/johnfarrar/two.html

hope that helps.


Post Your Comment







Show Captcha

If you subscribe, any new posts to this thread will be sent to your email address.

Copyright © 2007 Dan Vega | BlogCFC was created by Raymond Camden. This blog is running version 5.8.001.