
Associate values with list items 141
To do this, you populate the ComboBox control with an array of Object components. The
<mx:Object> tag lets you define a label property that contains the string to display in the
ComboBox, and a
data property that contains the data that you want to associate with the
label.
1. In the editor’s Source mode, replace the three <mx:String> tags with <mx:Object> tags,
as the following example shows:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
layout="absolute">
<mx:Panel x="10" y="10" width="350" height="200" title="Rate Customer
Service">
<mx:ComboBox id="cbxRating" x="20" y="20" width="100">
<mx:dataProvider>
<mx:Array>
<!-- These Object tags replace the String tags. -->
<mx:Object label="Satisfied" data="5"/>
<mx:Object label="Neutral" data="3"/>
<mx:Object label="Dissatisfied" data="1"/>
</mx:Array>
</mx:dataProvider>
</mx:ComboBox>
<mx:Button x="140" y="20" label="Send"/>
<mx:Label x="20" y="120" text="{cbxRating.value}"/>
</mx:Panel>
</mx:Application>
If you want to access the value of the selected item in the ComboBox control, you can use
the following expression in your code:
cbxRating.value
The value property contains the value of the selected item. When a data field is specified,
the
value property refers to the data field, not the label field. In the example, the
cbxRating.value property could contain the values 5, 3, or 1, depending on the user’s
selection.
2. Save the file, wait until Flex Builder finishes compiling, and then run the application.
Select items in the ComboBox control in the browser. The testing Label control you
inserted in the previous section displays the values 5, 3, or 1, depending on your selection.
To submit data for processing, you must use a remote procedure call (RPC) service
component. For a tutorial, see Chapter 18, “Use Web Services,” on page 205.
In this lesson, you inserted a list-based form control into your Flex application and populated
it with data. To learn more, see Chapter 12, “Using Data-Driven Controls” in Flex 2
Developer’s Guide.
Comentários a estes Manuais