2 May 2011
Beginning
In this exercise you will populate a ColumnChart control bound to value object instances stored in the employees ArrayCollection instance. You will also create a two-way binding. As you change the value in the Form container, you will update the data in the ArrayCollection object, which will immediately update the ColumnChart control (see Figure 1).
In this exercise, you will learn how to:
In this section, you will make the Employee ActionScript class bindable.
package valueObjects
{
[Bindable]
public class Employee
{
public var firstName:String;
public var lastName:String;
...
In this section, you will use a chart to display employee salary data.
Your Create Chart dialog box should appear as shown in Figure 3.
UI components comment, locate the ColumnChart control tags.ColumnChart tag, bind employees as the dataProvider.<mx:ColumnChart id="employeeChart"
dataProvider="{employees}">
<mx:series>
<mx:ColumnSeries displayName="Series 1"
yField=""/>
</mx:series>
</mx:ColumnChart>
series tag set, assign salary as the yfield.<mx:ColumnChart id="employeeChart" dataProvider="{employees}">
<mx:series>
<mx:ColumnSeries displayName="Series 1"
yField="salary"/>
</mx:series>
</mx:ColumnChart>
You should see the ColumnChart control populated with the employee salary data (see Figure 4).
In this section, you will create a form that will be used to display information about the employee item selected in the ColumnChart control.
You should see the ColumnChart control is populated with information and the Form container is empty (see Figure 8).
In this section you will use an event handler function to populate the Form container based on the item selected from the ColumnChart control.
itemClick event (see Figure 9).
ItemClick handler function for the itemClick event (see Figure 10).
Script block.Script block, below the employees variable declaration, type selectedEmployee and press Ctrl+1 to invoke the quick assist tool and select the Create instance variable 'selectedEmployee' option. This will create a private variable named selectedEmployee data typed to the Object class. Use the content assist tool (Ctrl+Space) to change the data type to the Employee class.selectedEmployee variable, use the content assist tool to add a Bindable metadata tag.[Bindable]
private var employees:ArrayCollection = new ArrayCollection();
[Bindable]
private var selectedEmployee:Employee;
Script block, locate the employeeChart_itemClickHandler() function.selectedEmployee variable. Cast the event.hitData.item object value to the Employee class using the as operand.protected function
employeeChart_itemClickHandler(event:ChartItemEvent):void
{
selectedEmployee = event.hitData.item as Employee;
}
UI components comment, locate the Form block.Form block, set the FormItem containers' label properties to First Name:, Last Name:, Location: and Salary:, respectively.<s:Form>
<s:FormItem label="First Name:">
<s:TextInput/>
</s:FormItem>
<s:FormItem label="Last Name:">
<s:TextInput/>
</s:FormItem>
<s:FormItem label="Location:">
<s:TextInput/>
</s:FormItem>
<s:FormItem label="Salary:">
<s:TextInput/>
</s:FormItem>
</s:Form>
FormItem container for the employee first name and bind its text property to the value selectedEmployee.firstName.<s:Form>
<s:FormItem label="First Name:">
<s:TextInput text="{selectedEmployee.firstName}"/>
</s:FormItem>
...
FormItem container for the employee last name and bind its text property to the value selectedEmployee.lastName.<s:Form>
<s:FormItem label="First Name:">
<s:TextInput text="{selectedEmployee.firstName}"/>
</s:FormItem>
<s:FormItem label="Last Name:">
<s:TextInput text="{selectedEmployee.lastName}"/>
</s:FormItem>
...
FormItem container for the employee location and bind its text property to the value selectedEmployee.location.<s:Form>
<s:FormItem label="First Name:">
<s:TextInput text="{selectedEmployee.firstName}"/>
</s:FormItem>
<s:FormItem label="Last Name:">
<s:TextInput text="{selectedEmployee.lastName}"/>
</s:FormItem>
<s:FormItem label="Location:">
<s:TextInput text="{selectedEmployee.location}"/>
</s:FormItem>
...
FormItem container for the employee salary and bind its text property to the value selectedEmployee.salary.<s:Form>
<s:FormItem label="First Name:">
<s:TextInput text="{selectedEmployee.firstName}"/>
</s:FormItem>
<s:FormItem label="Last Name:">
<s:TextInput text="{selectedEmployee.lastName}"/>
</s:FormItem>
<s:FormItem label="Location:">
<s:TextInput text="{selectedEmployee.location}"/>
</s:FormItem>
<s:FormItem label="Salary:">
<s:TextInput text="{selectedEmployee.salary}"/>
</s:FormItem>
</s:Form>
You should see the Form container is populated with the employee data from the item you clicked on in the ColumnChart control (see Figure 11).
You should see the ColumnChart control does not update when the Salary value is changed.
In this section you will create a two-way binding to the data that populates the chart.
FormItem container labeled Salary locate the TextInput control and assign its id property a value of salaryInput....
<s:FormItem label="Salary:">
<s:TextInput id="salaryInput"
text="{selectedEmployee.salary}"/>
</s:FormItem>
</s:Form>
<s:FormItem label="Salary:">
<s:TextInput id="salaryInput"
text="@{selectedEmployee.salary}"/>
</s:FormItem>
</s:Form>
You should see the ColumnChart control changes to reflect the salary entered (see Figure 12).
In this exercise you learned how to make an ActionScript class bindable, display the data model using a ColumnChart control and create a two-way binding. In the next exercise you will use Flash Builder to display details about the employee that is selected from a DropDownList control.
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License. Permissions beyond the scope of this license, pertaining to the examples of code included within this work are available at Adobe.