Blog of Steve Savage A blog for business analysts, and fellow users of Sparx's EA

12Jun/083

CFSelect: getting ‘bind’ to work with selected values

Problem addressed

This article provides a workaround allowing you to specify the selected items for cfselect when using the new AJAX bind function.

Solution

My example is a modification of the cfselect example taken from livedocs:

Upload fixColdfusionAjax.js to your site, and call the file using the script tag

<head>
 <title>your page</title>
 <script src="/{path}/fixColdfusionAjax.js></script>
</head>
<cffunction name="getValues" access="remote">
 <cfscript>
  VAR LOCAL = structNew();
  LOCAL.array = arrayNew(2);
  LOCAL.array[1][1] = "0";
  LOCAL.array[1][2] = "-- relationships --";
  LOCAL.array[1][3] = false; // not selected
  LOCAL.array[2][1] = "1";
  LOCAL.array[2][2] = "author";
  LOCAL.array[2][3] = true; // selected 
  LOCAL.array[3][1] = "2";
  LOCAL.array[3][2] = "publisher";
  LOCAL.array[3][3] = true; // selected (can have multiple values selected)
  return LOCAL.array;
 </cfscript>
</cffunction>

I will show a live example once I migrate this site to CF 8

Background

I've been slowly working on a new version of this site, and as part of the back end I wanted to use Coldfusion's new AJAX features for <CFForm>.

I quickly discovered that for some really bizarre reason, when you use the "bind" attribute to get the values for an option list <CFSELECT>
you can't indicate which options are selected.

I searched the net and found some work around's by Todd Sharp
and Ray Camdem but they seemed a bit awkward to me.

So, I decided to take a look at Coldfusion's AJAX javascript. After some digging I found out the problem was really simple to fix.

I've created the file fixColdfusionAjax.js, that contains JavaScript that over-rides a function from Coldfusion's cfajax.js code library to add support for selecting options

Comments (3) Trackbacks (0)
  1. Thank you! Works perfectly for what I was trying to do in CF 8.

  2. Hi-

    I copied the fixColdfusionAjax.js and placed it in the same directory and placed the following code:

    Places.cfm:

    your page

    Division:

    State:

    County:

    Places.cfc:

    SELECT IdDivision, DsDivision
    FROM Division
    ORDER BY DsDivision

    SELECT IdDivision, IdState, DsState
    FROM State
    WHERE IdDivision = #ARGUMENTS.Division#
    ORDER BY DsState

    SELECT IdDivision, IdState, IdCounty, DsCounty
    FROM County
    WHERE IdDivision = #ARGUMENTS.Division# AND
    IdState = #ARGUMENTS.State#
    ORDER BY DsCounty

    But Still It wont work, Can you help?

    http://www.lasanisarkar.org/bait/test/places.cfm

  3. @Tayyab Hussain, you need to place the fixColdfusionAjax.js after the body tag to make sure it loads after the Javascript inserted by ColdFusion.


Leave a comment

(required)

No trackbacks yet.