Populating related cfselects via Flash Remoting
First of let me say that as much as I would love this to be my code it mainly comes out of two posts [1,2] on ASFusion.com.
To sidetrack for a moment, I must point out now is that even if you don't find answers in the Asfusion posts always check the comments out! They get up to 50+ comments on some posts with hints and tips there as well.
Back to the form. I had a project which started life as a 5 page HTML form with the need to make it a little user friendly to the eyes and more organised. Any form over 2 pages I hate in general and I seem to be the only person in the world who thinks one long page is better than X amount of multiple pages this is whether i'm filling them in or creating them.
In moving my HTML form over to <cfform format="flash"> I ran into the problem of having to duplicate some Ajax where one select is populated from a DB then another select is populated from there. It didn't take too much digging around and I thought i'd share in case it helps anyone else out. My Flash/As knowledge isn't 100% so if you see an easier way drop me a line in the comments.
First of the CFCflashRemotingResponder.cfc
<cfcomponent name="flashRemotingResponder" access="public" description="Responds to Flash remoting requests"><cffunction name="getBrands" output="false" access="remote" returntype="query"> <cfset var qry = ""/> <cfset var brandQry = queryNew("brand_code,brand_name") /> <cfscript> queryAddRow(brandQry); querySetCell(brandQry,'brand_code','dell'); querySetCell(brandQry,'brand_name','Dell'); queryAddRow(brandQry); querySetCell(brandQry,'brand_code','apple'); querySetCell(brandQry,'brand_name','Apple Inc.'); queryAddRow(brandQry); querySetCell(brandQry,'brand_code','microsoft'); querySetCell(brandQry,'brand_name','Microsoft Corp.'); </cfscript> <cfquery dbtype="query" name="qry"> SELECT brand_code, brand_name FROM brandQry </cfquery> <cfreturn qry /></cffunction><cffunction name="getModels" access="remote" output="false" returntype="query"> <cfargument name="brand" type="string" /> <cfset var qry = ""/> <cfset var modelQry = queryNew("model_number,model_description,brand_code") /> <cfset var brand_code = arguments.brand /> <cfscript> queryAddRow(modelQry); querySetCell(modelQry,'model_number','123-qwe'); querySetCell(modelQry,'model_description','Apple Mac Book Pro 15.4"'); querySetCell(modelQry,'brand_code','apple'); queryAddRow(modelQry); querySetCell(modelQry,'model_number','987123-123'); querySetCell(modelQry,'model_description','Apple Mac Book Pro 17"'); querySetCell(modelQry,'brand_code','apple'); queryAddRow(modelQry); querySetCell(modelQry,'model_number','asdpo-1232'); querySetCell(modelQry,'model_description','Apple 72" Monitor'); querySetCell(modelQry,'brand_code','apple'); queryAddRow(modelQry); querySetCell(modelQry,'model_number','d123'); querySetCell(modelQry,'model_description','XPS Mobile Gaming Laptop'); querySetCell(modelQry,'brand_code','dell'); queryAddRow(modelQry); querySetCell(modelQry,'model_number','d6534'); querySetCell(modelQry,'model_description','64" Monitor'); querySetCell(modelQry,'brand_code','dell'); queryAddRow(modelQry); querySetCell(modelQry,'model_number','123-mqwe'); querySetCell(modelQry,'model_description','DRM removal tool'); querySetCell(modelQry,'brand_code','microsoft'); queryAddRow(modelQry); querySetCell(modelQry,'model_number','123-serr'); querySetCell(modelQry,'model_description','Windows Vista inc. SP4'); querySetCell(modelQry,'brand_code','microsoft'); </cfscript> <cfquery dbtype="query" name="qry"> SELECT model_number, model_description FROM modelQry WHERE brand_code = <cfqueryparam cfsqltype="cf_sql_varchar" value="#brand_code#" /> </cfquery> <cfreturn qry/></cffunction></cfcomponent>
Second the action <cfform>
flashRemoting.cfm
<cfset brandQry = queryNew("brand_code,brand_name") /><cfset modelQry = queryNew("model_number,model_description,brand_code") /><cfsavecontent variable="getBrands"> <cfoutput> //create connection var connection:mx.remoting.Connection = mx.remoting.NetServices.createGatewayConnection("http://#cgi.HTTP_HOST#/flashservices/gateway/"); //declare service var myService:mx.remoting.NetServiceProxy; </cfoutput> var responseHandler = {}; //put the controls in scope to avoid calling _root var brand = brand; responseHandler.onResult = function( results: Object ):Void { //when results are back, populate the cfselect brand.labelField = "brand_name"; brand.dataProvider = results; } responseHandler.onStatus = function( stat: Object ):Void { //if there is any error, show an alert alert("Error while calling cfc:" + stat.description); } //get service myService = connection.getService("flashRemotingResponder", responseHandler ); //make call myService.getBrands();</cfsavecontent><cfsavecontent variable="getModel"> <cfoutput> //create connection var connection:mx.remoting.Connection = mx.remoting.NetServices.createGatewayConnection("http://#cgi.HTTP_HOST#/flashservices/gateway/"); //declare service var myService:mx.remoting.NetServiceProxy; </cfoutput> var responseHandler = {}; //put the controls in scope to avoid calling _root var model = model; responseHandler.onResult = function( results: Object ):Void { //when results are back, populate the cfselect model.labelField = "model_description"; model.dataProvider = results; } responseHandler.onStatus = function( stat: Object ):Void { //if there is any error, show an alert alert(stat.description); } //get service myService = connection.getService("flashRemotingResponder", responseHandler ); //make call myService.getModels(brand.selectedItem.brand_code);</cfsavecontent><cfform format="flash" name="myform" onload="#getBrands#" width="500" height="400"> <cfformgroup type="panel" label="Example of flash remoting and dynamically populating cfselect"> <cfselect name="brand" id="brand" query="brandQry" display="brand_name" value="brand_code" onchange="#getModel#" label="What is the brand name"></cfselect> <cfselect name="model" id="model" query="modelQry" display="model_description" value="model_number" label="What Model Number"></cfselect> </cfformgroup></cfform>
You can download the source at: http://www.andyjarrett.co.uk/flash_remoting_populate_select.zip