Link to home
Start Free TrialLog in
Avatar of kpjj31
kpjj31

asked on

populate select menu with array from another select menu

I need to learn how to populate one select menu from the information from another. I have a select menu that is populated form a ColdFusion component using CFINVOKE. The data that is being return has nested arrays that go with the value in the select menu. I need to populate a second select menu with the array of the selected item form the first select menu. I have a attached word doc that show what I am doing.
example.docx
Avatar of GuruJava
GuruJava

Hi,

 maybe this is what you are looking for

This one is 'index.cfm'
<cfquery name="qstates" datasource="info">
  SELECT states
  FROM info

  GROUP BY states
</cfquery> 
<html> 
<head> 
</head> 
<body> 
<cfform>
<cfselect name="DropDown1" bind="cfc:BinFcns.Method({DropDown1})">
    <cfoutput query="qstates"><option>#states#</option></cfoutput>
</cfselect>
<cfselect name="DropDown2" bind="cfc:BinFcns.Method({DropDown1})" /> 
</cfform>
</body> 
</html>

Open in new window


and this one is the .cfc, 'BinFcns.cfc'

<cfcomponent output="false">
   <!--- set function name --->
   <cffunction name="Method" access="public" returnType="array">
    <!--- this is what you passed to the CFC via the {} think in the select --->
      <cfargument name="Selected" type="numeric" required="true">
      <!--- Define array to produce the drop down --->
      <cfset var data="">
      <cfset var result=ArrayNew(2)>
      <cfset var i=0>
      <!--- Get data --->
      <cfquery name="data" datasource="#THIS.dsn#">
      SELECT *
      FROM 2ndDropDownData
      Order by DataName
      </cfquery> 
      <!--- Convert results to array --->
      <cfloop index="i" from="1" to="#data.RecordCount#">
             <cfset result[i][1]=data.DropDownID[i]>
             <cfset result[i][2]='#DropDownTEXT#'>      
             <!--- determine which is selected via what you passed and something in the database --->
            <cfif data.DropDownID[i] eq #Selected#>
                <cfset result[i][3]=true>
            </cfif>
      </cfloop>

      <!--- And return it --->
      <cfreturn result>
   </cffunction>
</cffunction>  

Open in new window


<cfselect name="DropDown2" bind="cfc:cfcname.Method({DropDown1})" />
ASKER CERTIFIED SOLUTION
Avatar of _agx_
_agx_
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Just noticed a typo in the CFC example.  The code will work as is, but in the interest of best practices, the variables in these 2 lines:

      <cfreturn regionQuery />
        ....
      <cfreturn areaQuery/>

... should be replaced to include the variable scope, ie LOCAL:

      <cfreturn Local.regionQuery />
        ....
      <cfreturn Local.areaQuery/>