23 May 2011
Beginning
In this exercise you will use multiple methods to apply item editors and item renderers to a DataGrid control as shown in Figure 1.
By default, an item renderer is displayed as a text field. In this section, you will create an inline item renderer that displays employee evaluation scores in a customized text field.
This is the application you built in the previous exercise. You should see the application consists of a DataGrid control with two columns, Name and Hire Date (see Figure 2).
UI components comment, locate the columns property that contains the ArrayList instance within the DataGrid control.GridColumn control that displays the employee hire dates, add another instance of the GridColumn control....
<s:columns>
<s:ArrayList>
<s: GridColumn headerText="Name"
labelFunction="employeeName"/>
<s:GridColumn dataField="hireDate"
headerText="Hire Date"
labelFunction="dateFormat"/>
<s:GridColumn/>
</s:ArrayList>
</s:columns>
GridColumn control, add the dataField property with a value of evaluation and the headerText property with a value of Evaluation....
<s:GridColumn dataField="evaluation"
headerText="Evaluation"/>
...
The dataField property references the evaluation data from the employees.xml file in the data package of this propject. This data represents the quarterly performance evaluation score for each employee.
Notice that the Evaluation column is too small to see the header (see Figure 3).
DataItem control within the typicalItem property.hireDate, add the evaluation object and set its value to the full length of the headerText, Evaluation.<s:DataItem firstName="Christopher"
lastName="Winchester"
hireDate="12/31/2011"
evaluation="Evaluation"/>
Notice that the Evaluation column is now large enough to display the whole header and all of the data (see Figure 4).
GridColumn instance from single tag syntax to block syntax.<s:GridColumn dataField="evaluation"
headerText="Evaluation">
</s:GridColumn>
GridColumn block, add an inline itemRenderer property block.<s:GridColumn dataField="evaluation"
headerText="Evaluation">
<s:itemRenderer>
</s:itemRenderer>
</s:GridColumn>
itemRenderer block add a Component block.Component block, add a GridItemRenderer tag block.<s:itemRenderer>
<fx:Component>
<s:GridItemRenderer>
</s:GridItemRenderer>
</fx:Component>
</s:itemRenderer>
GridItemRenderer block, add a Label control.Label control, add a text property that binds the value of the data property referencing the evaluation data. All data stored in the dataProvider property is referenced through the data property of the DataGrid control.<s:Label text="{data.evaluation}"/>
You are adding the text property to the Label control and binding it to display the data from the dataField property in the GridColumn control. In this case, you are displaying the evaluation data.
height property with a value of 100% and the right property with a value of 10.verticalAlign property with a value of middle, the fontWeight property with a value of bold, and the fontStyle property with a value of italic.<s:Label text="{data.evaluation}"
height="100%"
right="10"
verticalAlign="middle"
fontWeight="bold"
fontStyle="italic"/>
You can see that the text in the Evaluation column of the DataGrid control has been formatted and still displays the evaluation data (see Figure 5).
In this section, you will create a new component item renderer to display employee images.
The New Item Renderer dialog box should look like Figure 6.
The new EmployeeDisplay item renderer will open in the Editor view. Note that the EmployeeDisplay.mxml file extends the GridItemRenderer class.
GridItemRenderer tag, add the width and height properties and set their values to 100.EmployeeDisplay custom component file, remove everything within the GridItemRenderer tag block.GridItemRenderer block, create an instance of the VGroup.text property and use the firstName and lastName data objects as the value.<s:VGroup>
<s:Label text="{data.firstName} {data.lastName}"/>
</s:VGroup>
Label control, add a BitmapImage control and set the source property value to images/{data.id}.jpg.<s:VGroup>
<s:Label text="{data.firstName} {data.lastName}"/>
<s:BitmapImage source="images/{data.id}.jpg"/>
</s:VGroup>
labelFunction property from the GridColumn instance that displays the employee's name.GridColumn, add the itemRenderer property and set its value to point to the EmployeeDisplay custom component in the components package.<s:GridColumn headerText="Name"
itemRenderer="components.EmployeeDisplay"/>
You should see the Name column of the DataGrid control, now displays both the employee names and images. Notice that the names and images are not centered within the column. Also notice that you may not be able to see the whole DataGrid control in the browser (see Figure 7).
DataGrid control, add a height property with a value of 600.VGroup container, add the horizontalAlign property with a value of center and the verticalAlign property with a value of middle.width and height properties to the VGroup definition and set both values to 100%.You should see that the employee data in the Name column is now centered within the column and the DataGrid control is contained within the browser (see Figure 8).
As you know, the display that you view in a cell of the DataGrid control is an item renderer. When you double-click on an editable cell, you are presented with the display for an item editor. Like the item renderer, by default, the item editor is a text field. In this section, you will apply the NumericStepper control as an item renderer and then set it to display as the item editor as well
DataGrid control, add the editable property and set its value to true.Notice that every item in the DataGrid is editable if you double-click it (see Figure 9).
GridColumn control with the Name headerText, add the editable property and set it to false.GridColumn control with the Hire Date headerText.You should see that only the Evaluation column is editable.
GridItemRenderer for the Evaluation column and comment out the Label control within the block.GridItemRenderer block, add an instance of the NumericStepper control.The Evaluation column now displays a NumericStepper control for the item renderer but all of the values are set to zero as shown in Figure 10.
value property to the NumericStepper control with a value that binds the data.evaluation data.<s:NumericStepper value="{data.evaluation}"/>
The Evaluation column now shows the correct data (see Figure 11). Notice that you can change the value in the NumericStepper, but if you change the value to a number larger than 10, it switches the value back to 10. This is because the NumericStepper has a default maximum value of 10.
NumericStepper, add the width property with a value of 100% and add the maximum property with a value of 100.<s:NumericStepper value="{data.evaluation}"
width="100%"
maximum="100"/>
The maximum number in the NumericStepper is now 100. If you type a number greater than 100, the NumericStepper will switch the number to 100 as it did before.
NumericStepper.Notice that the item editor is still a text field (see Figure 12).
GridColumn control, add the rendererIsEditable property and set the value to true.<s:GridColumn dataField="evaluation"
headerText="Evaluation"
rendererIsEditable="true">
You no longer see a text field when you double-click the NumericStepper control because the item editor is now the NumericStepper control defined in the item renderer.
In this section you will use the itemEditor property to create an inline item editor.
GridColumn instance, remove the rendererIsEditor property.itemRenderer property and change it to the itemEditor property.GridItemRenderer instance to GridItemEditor.You should see a warning that says, "Data binding will not be able to detect assignments to 'data'."
Notice when you try to edit the last column, the value in the NumericStepper is zero. Also notice that when you change the number, it returns to the original evaluation score.
NumericStepper control, change the value property to @{value}.<s:NumericStepper value="@{value}"
width="100%"
maximum="100"/>
The "value" in the binding is a property of the GridItemEditor instance and holds the value of the data for the data provider of this cell. In this case, that data is the evaluation value. You use @ to create a two-way binding so that when a user updates the NumericStepper, the data in the data provider updates, too.
You should see the warning goes away.
Now when you edit the Evaluation column, the value in the NumericStepper starts at the original evaluation score. When you change the value in the NumericStepper, the value is properly updated in the column.
In this section, you will create a new component file for the item editor you created in the last section.
The New MXML Component dialog box should appear as shown in Figure 13.
GridItemEditor block within the itemEditor property block.NumericStepper control and paste it under the Declarations block in the EmployeeEvaluation.mxml file.itemEditor property block and its contents.GridColumn instance so that it uses single tag syntax.GridColumn instance, add the itemEditor property and set its value to reference the EmployeeEvaluation file in the components package.<s:GridColumn dataField="evaluation"
headerText="Evaluation"
itemEditor="components.EmployeeEvaluation"/>
You should see that there is no change to the application.
In this section, you will add the selectionChange event to the DataGrid control and handle a user selection of a DataGrid item.
DataGrid control, add the selectionChange event.<s:DataGrid dataProvider="{employeeList}"
alernatingRowColors="[#FFFFFF, #CCCCCC]"
height="600"
editable="true"
selectionChange="datagrid1_selectionChangeHandler(event)">
datagrid1_selectionChangeHandler() method in the Script block.protected function datagrid1_selectionChangeHandler(event:GridSelectionEvent): void
{
}
datagrid1_selectionChangeHandler() function, remove the generated code stub and use content assist (CTRL+Space) to add the Alert.show() method.protected function datagrid1_selectionChangeHandler(event:GridSelectionEvent)
{
Alert.show();
}
Alert.show() method, then the Alertcontrol should have been automatically imported. If you do not see the import statement, manually add the import statement for the control.import mx.controls.Alert;
Alert.show() method, add the string You selected, followed by a literal space, concatenated with the value.Alert.show() method, add a title heading that reads Employee Portal: Notification.protected function datagrid1_selectionChangeHandler(event:GridSelectionEvent)
{
Alert.show("You selected " + event.currentTarget.selectedItem.firstName, "Employee Portal: Notification");
}
You should see an alert message appear for the employee you selected (see Figure 15).
In this exercise you used multiple methods to apply item editors and item renderers to a DataGrid 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.