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

cj_ervinAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

cj_ervinAuthor Commented:
Got it working..... Added script for fixColdfusionAjax.js


ColdFusion.Bind.assignValue=function(_31a,_31b,_31c,_31d){
/* this function has been modifed from cfajax.js 
and is 
Copyright 2007 Adobe Systems Incorporated
All Rights Reserved.

Only one line was need for the fix, search for
"this is the line I changed"
*/


	var $C=ColdFusion;
	var $U=$C.Util;
	var $E=$C.Event;
	var $L=$C.Log;
	// _31a = bindTo, _31b = bindToAttr, _31c = ?, _31d = bindToParams
	if(!_31a){return;}
	if(_31a.call){_31a.call(null,_31c,_31d); return;}
	var _31e=$C.objectCache[_31a];
	if(_31e&&_31e._cf_setValue){_31e._cf_setValue(_31c);return;}
	var _31f=document.getElementById(_31a); // get a pointer to the element
	if(!_31f){$C.handleError(null,"bind.assignvalue.elnotfound","bind",[_31a]);}
	if(_31f.tagName=="SELECT"){
		var _320=$U.checkQuery(_31c); // should return "null" since this is an array not a query.
		var _321=$C.objectCache[_31a]; // a pointer to the bound object
		if(_320){ // triggered if this is a query
			if(!_321||(_321&&(!_321.valueCol||!_321.displayCol))){
				$C.handleError(null,"bind.assignvalue.selboxmissingvaldisplay","bind",[_31a]);
				return;
			}
		}else{ // an array was passed
			if(typeof (_31c.length)=="number"&&!_31c.toUpperCase){
				if(_31c.length>0&&(typeof (_31c[0].length)!="number"||_31c[0].toUpperCase)){
					$C.handleError(null,"bind.assignvalue.selboxerror","bind",[_31a]);
					return;
				}
			}else{
				$C.handleError(null,"bind.assignvalue.selboxerror","bind",[_31a]);
				return;
			}
		}
			_31f.options.length=0;
			if(!_320){ // for the array
				for(var i=0;i<_31c.length;i++){
					// this is the line I changed
					//var opt=new Option(_31c[i][1],_31c[i][0]);
					var opt=new Option(_31c[i][1],_31c[i][0],false,_31c[i][2]);
					_31f.options[i]=opt;
				} // if !320
			}else{ // for queries
				if(_320=="col"){
					var _324=_31c.DATA[_321.valueCol];
					var _325=_31c.DATA[_321.displayCol];
					if(!_324||!_325){
						$C.handleError(null,"bind.assignvalue.selboxinvalidvaldisplay","bind",[_31a]);
						return;
					} // if !_324
					for(var i=0;i<_324.length;i++){
						var opt=new Option(_325[i],_324[i]);
						_31f.options[i]=opt;
					}
				} // if _320
				else{
					if(_320=="row"){
						var _326=-1;
						var _327=-1;
						for(var i=0;i<_31c.COLUMNS.length;i++){
							var col=_31c.COLUMNS[i];
							if(col==_321.valueCol){
							_326=i;
						} // for
						if(col==_321.displayCol){_327=i;} 
						if(_326!=-1&&_327!=-1){break;}
					} // if
					if(_326==-1||_327==-1){
						$C.handleError(null,"bind.assignvalue.selboxinvalidvaldisplay","bind",[_31a]);
						return;
					} // if
					for(var i=0;i<_31c.DATA.length;i++){
						var opt=new Option(_31c.DATA[i][_327],_31c.DATA[i][_326]);
						_31f.options[i]=opt;
					} // for
				}
			}
		}
	}else{
		_31f[_31b]=_31c;
	}
	$E.callBindHandlers(_31a,null,"change");
	$L.info("bind.assignvalue.success","bind",[_31c,_31a,_31b]);
};

Open in new window



Now it works perfectly!!


Leaving this on here in case anyone else needs this issue fixed. I searched for hours and hours to find a solution with no avail. I searched some old code, thinking I had done this before, and I found where I had and found what I did to fix it last time. I hope this helps someone else.
1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
_agx_Commented:
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.
0
cj_ervinAuthor Commented:
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" />
1
_agx_Commented:
> 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.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ColdFusion Language

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.