How to populate a cfselect with Flash Remoting

Just because there are few tricky issues when populating a cfselect with Flash Remoting, I made this simple example. There are two ways, one requires our query to contain the columns “data” and “label”, in which case, when we receive the result, we do the same as the cfgrid example:


Just because there are few tricky issues when populating a cfselect with Flash Remoting, I made this simple example. There are two ways, one requires our query to contain the columns “data” and “label”, in which case, when we receive the result, we do the same as the cfgrid example:

mycfselect.dataProvider = results;

In this way, we continue accessing the selected value with mycfselect.selectedItem.data as usual.

With the other way, we can keep the columns of our query, and when we receive the results, we specify the column that will be the label:

mycfselect.labelField = "myColumnName";

Note that ActionScript is case sensitive, so when we access these columns, we need make sure we write correctly.

Now, we can access any other column that was provided in the query by
mycfselect.selectedItem.myColumnName, and unless our query contains a “data” column we will not be get anything if we write mycfselect.selectedItem.data.

The function that handles the request:

responseHandler.onResult = function( results: Object ):Void { //when results are back, populate the cfselect contactList.labelField = "name"; contactList.dataProvider = results; }

And the form:

Update: If we want to select a particular item when the data is returned, we will have to loop through the items returned looking for the right one. The item to be selected can come from many sources, but to make an easy example, we'll get it from a text field. One recommendation though is not to set it in the ActionScript code if it will dynamically change, but set it in a hidden field instead. So the code for that will be:

//previous code here //put the controls in scope to avoid calling _root var contactList = contactList; var idField = idField; responseHandler.onResult = function( results: Object ):Void { //when results are back, populate the cfselect contactList.labelField = "name"; contactList.dataProvider = results; //select a specific item for (var i = 0; i < contactList.dataProvider.length; i++){ if (contactList.getItemAt(i).id == idField){ contactList.selectedIndex = i; break; } } }

Add some field to get the id to select

A live example
Download the source

We will also make an example of two or more cfselect related with Remoting.