Link to home
Start Free TrialLog in
Avatar of cj_ervin
cj_ervin

asked on

cfselect bind Not return selected value in dropdown

After user selects a company, the bind on the state should go to the CFC, and start by quering table to get all states.

Then query to get the State in the Company address.

Then return array result to the cfselect all the states in the dropdown, but select the State for the Address of the company selected in the first dropdown

I get all the states returned, but the state in the company address does not get selected in the dropdown.

	<!--- AUTO SUGGEST WORKS FINE --->
	<cfinput type="Text" class="form-field-look" name="Company_Name" required="Yes" message="You Must Enter Value" autosuggest="cfc:cfc_GetCompanyName.getCompanyNames({cfautosuggestvalue})">
	
	<!--- DROPDOWN NEEDS UPDATED WITH ALL STATES AND THEN DEPENDING ON THE CHOICE ENTERED (SELECTED) ABOVE THEN THE STATE FIELD SHOULD BE 
	POPULATED WITH ALL THE STATES AND THAT STATE IN THE ADDRESS SHOULD RETURN AS SELECTED FOR THE DROPDOWN --->
	<cfselect name="State"  bind="cfc:cfc_GetCompanyData.getCompanyState({Company_Name})" bindonload="Yes"></cfselect>

Open in new window


<cffunction name="getCompanyState" access="remote" returnType="array">
		<!--- PASSED THROUGH FOR THE QUERY SEARCH BELOW --->
		<cfargument name="CompanyName" type="string" required="true">
	
			<!--- Define variables --->
			<cfset var data="">
			<cfset var result=ArrayNew(2)>
			<cfset var i=0>
			
			<cfset result[1][1]= "N/A">
			<cfset result[1][2]= "">
			
			<!--- GET LIST OF ALL STATES --->
			<cfquery name="data" datasource="#application.DSNName#">
				SELECT Abb, State
				FROM States
				ORDER BY State
			</cfquery>
			
			<!--- GET THE STATE IN THE COMPANY ADDRESS --->
			<cfquery name="qGetCompanyState" datasource="SCCS">
				SELECT State
				FROM Clients
				WHERE Company_Name = '#ARGUMENTS.CompanyName#'
			</cfquery>
			
			<cfloop index="i" from="1" to="#data.RecordCount#">
				<cfset result[i+1][1] = data.Abb[i]>
				<cfset result[i+1][2] = data.Abb[i]>
				<!--- IF TRUE IT SHOULD SET VALUE AS TRUE AND RETURN TO CFSLECT AS THE SELECTED ITEM IN THE DROPDOWN --->
				<!--- IF THE COMPANY STATE IS EQUAL TO ONE OF THE STATES IN THE DROPDOWN THEN IT SHOULD BE THE ONE SELECTEDIN THE DROPDOWN ON RETURN --->
				<cfset result[i+1][3] = IIF(qGetCompanyState.State eq data.Abb,DE(true),DE(false))>
			</cfloop>
	
		
		<!--- And return it --->
		<cfreturn result>
	</cffunction>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of cj_ervin
cj_ervin

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
Avatar of _agx_
Thanks for posting the solution.  Just keep in mind future updates, may possibly overwrite the changes to the js files.  

Out of curiosity, what version are you using? Not sure, but I could've sworn they added support for "selected" with binds at some point.
Avatar of cj_ervin
cj_ervin

ASKER

Your welcome. Good point, thanks for pointing that out.

One client still has CF9 and another has CF11. As far as I know, they did add support for "selected" in CF9, if the "select" value is passed from the form to the CFC, then it will select the correct one. But if you do not know what should be selected till inside the CFC, then the "selected"  must be passed back via the CFC.

Here is sample code for the CFC bind in the cfselect

<cfselect name="cfSelectName" display="DisplayName" selected="#form.SelectedID#" bind="cfc:CFCName.getCFCFunction()" bindOnLoad="true" />
> if you do not know what should be selected till inside the CFC

Oh, yeah - duh!  In cases like yours it needs to be evaluated at run time.  Unfortunately, the "selected" value is only evaluated once when the page is generated, so it wouldn't be dynamic enough in this case.