|
Comments
Did you read today's front page stories & breaking news?
SYS-CON.TV
|
AJAXWorld News Desk ColdFusion AJAX Tutorial 2: Related Selects
Many of us have built related select controls, forms with two (or more) drop down controls
By: Ben Forta
Jun. 4, 2007 10:45 PM
Many of us have built related select controls, forms with two (or more) drop down <SELECT> controls, where making a change in one control causes the available selections in the related control to change. For example, selecting a category in one control displays category products in a related control, or selecting a state in one control updates a related control with the cities in that state.
These controls are typically implemented using client side JavaScript to process arrays of data embedded in the page itself. Every possible combination and option is embedded in JavaScript in the page, and client side scripts update controls based on selection changes in other controls. ColdFusion 8's new Ajax functionality makes this kind of interface really easy, without requiring any client-side scripting, and without requiring that all of the data be embedded in the generated page. Rather, <CFSELECT> controls may be bound to ColdFusion Component methods that are asynchronously invoked as needed. To demonstrate this, here is a complete working example which uses one of the example databases that comes with ColdFusion. First the ColdFusion Component: <cfcomponent output="false"> <cfset THIS.dsn="cfartgallery"> <!--- Get array of media types ---> <cffunction name="getMedia" access="remote" returnType="array"> <!--- Define variables ---> <cfset var data=""> <cfset var result=ArrayNew(2)> <cfset var i=0> <!--- Get data ---> <cfquery name="data" datasource="#THIS.dsn#"> SELECT mediaid, mediatype FROM media ORDER BY mediatype </cfquery> <!--- Convert results to array ---> <cfloop index="i" from="1" to="#data.RecordCount#"> <cfset result[i][1]=data.mediaid[i]> <cfset result[i][2]=data.mediatype[i]> </cfloop> <!--- And return it ---> <cfreturn result> </cffunction> <!--- Get art by media type ---> <cffunction name="getArt" access="remote" returnType="array"> <cfargument name="mediaid" type="numeric" required="true"> <!--- Define variables ---> <cfset var data=""> <cfset var result=ArrayNew(2)> <cfset var i=0> <!--- Get data ---> <cfquery name="data" datasource="#THIS.dsn#"> SELECT artid, artname FROM art WHERE mediaid = #ARGUMENTS.mediaid# ORDER BY artname </cfquery> <!--- Convert results to array ---> <cfloop index="i" from="1" to="#data.RecordCount#"> <cfset result[i][1]=data.artid[i]> <cfset result[i][2]=data.artname[i]> </cfloop> <!--- And return it ---> <cfreturn result> </cffunction> </cfcomponent> This CFC contains two methods. getMedia returns all of the media types in the art catalog database, and getArt accepts a media id and returns any art that is associated with that passed id. Both methods convert their results into two dimensional arrays, with the first dimension containing the id (to be used as the value in the <SELECT> control) and the second containing the display text. (For now, this two dimensional array is the format required by <CFSELECT>). Now for the form itself: <cfform> <table> <tr> <td>Select Media Type:</td> <td><cfselect name="mediaid" bind="cfc:art.getMedia()" bindonload="true" /></td> </tr> <tr> <td>Select Art:</td> <td><cfselect name="artid" bind="cfc:art.getArt({mediaid})" /></td> </tr> </table> </cfform> The form contains two <CFSELECT> controls, one named "mediaid" and the other named "artid". "mediaid" is bound to cfc:art.getMedia(), and so to obtain the list of media types to populate the control, the client makes an asynchronous call to the getMedia method in art.cfc, and populates the list with the returned array. As we'd want this control to be automatically populated when the form loads, bindonload is set to "true", this way the getMedia() call is fired automatically at form load time. "artid" is bound to the getArt method in art.cfc. This method requires that a mediaid be passed to it, and so {mediaid} is used so as to pass the currently selected value of control mediaid (the first <CFSELECT>). Because these two controls are bound together, the second dependant on the first, ColdFusion automatically generates JavaScript code that forces artid to be repopulated with newly retrieved data whenever mediaid changes. This example binds just two controls, but this mechanism can be used to relate as many controls as needed, and not just <CFSELECT> controls either. Reader Feedback: Page 1 of 1
Your Feedback
Latest Cloud Developer Stories
Subscribe to the World's Most Powerful Newsletters
Subscribe to Our Rss Feeds & Get Your SYS-CON News Live!
|
SYS-CON Featured Whitepapers
Most Read This Week
Breaking Cloud Computing News
|
||||||||||||||||||||||||||||||||||||||||||||||||||||