The Flash application you'll create will have two data grids, one on top of the other. The upper grid will display the departments, and each time a user clicks a department, the lower grid will display the department employees.
Creating a data-driven grid requires using three Flash components: the grid itself; the WebServiceConnector component, which communicates with the ColdFusion component; and a DataSet, which binds the two providing other Flash components and code with access to retrieved data. You could manually drag these three components from the Flash Components panel to the Stage, provide the required properties, and then bind them together. Or you can use the Data Connection Wizard to do much of the work for you. Here are the steps to follow:
Select Commands > Data Connection Wizard. This launches the wizard (Figure 5).
Figure 5.The Data Connection Wizard simplifies connecting to web services and XML data
ListDepts
to populate the grid.)Add a second text field and name it DEPARTMENTNAME with a data type of String (Figure 6).
Figure 6. If using a ColdFusion Component, field names must be specified manually
You must name component instances in Flash. Specify dept as the Label for Generated Components. Leave the two checkboxes selected (the first instructs the wizard to create a data grid, and the second embeds a button that a user may click to execute the web service call) as in Figure 7.
Figure 7. The wizard can create optional DataSets and trigger buttons.
You should now have four components on the Stage: a WebServiceConnector, a DataSet, a DataGrid, and a Button. Click any of the first three and look at the tabs in the Component Inspector panel. These are the values that you would have had to specify manually were it not for the Data Connection Wizard. (You will have to use the Component Inspector panel later).
Now test the application:
An empty grid will display. To populate the grid, click the Get
Data button; Flash will connect to ColdFusion through the WebServiceConnector,
retrieve the data from the ColdFusion component ListDepts
method, and use the results to populate the DataSet, which populates
the DataGrid (Figure 8).
Figure 8. A working DataGrid, no code used
Not bad at all—for no lines of code that is.
The default grid is too big—it leaves no room for or next grid—so set the size and position:
If the Properties panel is not open, open it (Figure 9).
Figure 9. Control size and position is defined in the Property inspector.
Before moving on to the next data grid, make two changes in the department grid; right now, the interface displays both the department name and id, when you only need to display the department name. Moreover, your grid should populate dynamically without requiring the user to press a button.
You'll tackle the displayed columns first. By default, data grids display all columns in their related data sets; to change this behavior, use ActionScript code. Fortunately, a wizard can generate this code for you. Follow these steps:
Click the wizard button (the yellow star icon) on the top right of the panel, and select DataGrid Column Editor to launch a wizard (Figure 10).
Figure 10. The DataGrid Column Editor wizard generates grid layout code.
Save your changes and select Control > Test Movie to launch the application. Your revised grid appears in its correct location and with just one column (Figure 11).
Figure 11. To include, exclude, or format grid columns, use ActionScript code.
The final change is to delete the Get Data button and to write some code to force the grid to populate automatically upon starting. Here are the steps:
Type the following code in the Actions panel:
// Populate grid on startup
on (reveal) {
deptReceiveWebServiceConnector.trigger();
}
The code you type warrants some explanation.
Flash supports events, and code associated with those events executes when certain
events occur. In
this case, the on(reveal) event instructs Flash to execute
the following code when the current form (the data form) reveals or
displays itself. The deptReceiveWebServiceConnector.trigger()
event simply triggers or executes the deptReceiveWebServiceConnector
service. This is exactly what the Get Data button performed.
Next, you'll create the employees grid; this will display the employees in the department selected in the first grid. Once again, start with the wizards:
ListByDept
method to display its attributes and results.ListByDept
to populate the grid).You must reposition the grid::
Select the new data grid (named empDataGrid) and set the position and size in the Property inspector as follows: W=510, H=175, X=20, Y=180 (Figure 12).
Figure 12. Use the Property inspector to precisely position controls.
The application will run and display the new grid, but that grid will not be populated because the app never executes the second web service. To execute the web service, you must first pass the appropriate department id to the method:
Options in the Bound To dialog box display hierarchically. Bind to the currently selected department id in the department DataSet by locating the deptDataSet and selecting it; then select DEPARTMENTID from the list on the right (Figure 13).
Figure 13. The Bound To dialog is used to interactively select data bindings
You must execute the ListByDept each time the selection in deptDataSet changes. Do so with the following steps:
Type the following code in the Actions panel:
// Get employees when department is selected
on (iteratorScrolled) {
_parent.empReceiveWebServiceConnector.trigger();
}
The iteratorScrolled event executes each time the selection
in a DataSet changes. The code here executes the empReceiveWebServiceConnector
web service. The _parent is necessary because references
are all relative. The DataSet and the WebServiceConnector are both children
of the data form, so _parent refers to the form,
and empReceiveWebServiceConnector then refers to the connector
within it. It is worth noting that the following code would have worked
too:
// Get employees when department is selected
on (iteratorScrolled) {
_root.application.data.empReceiveWebServiceConnector.trigger();}
The _root refers to the top of the application tree; application
is the top level form; data is a child of application,
and empReceiveWebServiceConnector is a child of data.
The result is identical, the only difference is that this code uses
absolute addresses whereas the previous example uses relative addresses.
To keep the steps simple, I did not use the DataGrid Column Editor wizard,
causing all the columns to display using the column names as headers.
You may use the wizard if you desire to modify the format; for instance,
the finished app probably should not display the EMPLOYEEID
and DEPTIDFK columns (as a user should never see or edit
these).