So far you have a working application which displays a master/detail view of two related tables, without typical browser refreshes. That is pretty impressive in and of itself. But more impressive would be the ability to add, edit, and delete records all from within the grid.
The truth is, to make a Flash grid editable is really easy. For instance,
editable is a grid property; just set it to true
and you have an editable grid. But you'd probably not want every user
to be able to edit data, and so next you'll implement a login screen.
All users will be able to browse data, but edits will require a log-on.
The first thing you'll do is create the login screen. Here are the steps:
This new form will contain the login screen. Double-click the default name and change it to login (Figure 14).
Figure 14. Each form in a form application must be uniquely named.
You now have a complete form (you'll wire it up shortly). For now, save the changes and test the application.
Figure 15. The login form contains two labels, two input components, and two buttons.
Figure 16. Flash Forms are "transparent," and will show through each other by default.
You'll find that the application works, actually, a bit too well. A seen in Figure 16, the new login form displays automatically on startup and overlays the grids; obviously, that is not the correct behavior. By default, all new forms will display when the application starts. To prevent the login form from initially displaying, do the following:
When working with multiple forms, here's a useful tip: Right-click any form in the tree and select Hide Screen to prevent it from appearing while you work on other forms. This has no impact on runtime execution, it only affects visibility within the Flash authoring tool.
You must display a login screen only when you need a log-on. You'll add a login button and display the login form when it a user requests information that requires a log-on. Here are the steps:
Type the following code in the Actions panel (Figure 17):
// Display login form
on (click) {
_root.application.login.visible=true;
}
Figure 17. Flash associates code in the Actions panel with the currently selected form or component
That worked—the form displayed, but the UI is clumsy. The grids show through the form, and users can use them while the login form appears. What you really want is for the form to display in a pop-up window. This is feasible, but requires some manual coding. Here is what you need to do:
Go back to the Actions panel for the buttonLogin, remove the code you just inserted, and enter the following code instead:
// Popup login form when button pressed
on (click) {
// Work out position
var x = (_root.application.width-300)/2;
var y = (_root.application.height-150)/2;
// Create popup
var loginWindow = mx.managers.PopUpManager.createPopUp(_root,
mx.containers.Window,
true,
{contentPath:"login", title:"Login", _x:x, _y:y});
// Set window size
loginWindow.setSize(300, 150);
// Save window instance (so it can be closed later)
_root.application.login.loginInstance=loginWindow;
}
Of course, you don't actually need the Window component on the Stage, so delete it. If that sounds a bit awkward, well, it is. Open the Library panel (Figure 18) if you are so inclined; it lists the components currently in use, and you'll see that it lists Window (even if you deleted it).
Figure 18. Use the Library panel to see the components used by an application.
So what does the ActionScript code that you just used actually do?
The on(click) traps button clicks so that the code that
follows will execute when a user presses Login. The first two lines
of code define two variables, x and y, which contain the position of
the pop-up window (calculated by taking the application height and width
minus the pop-up window height and width divided by two). The next line
of code uses the PopUpManager class to create a pop-up
window, specifying login as the name of the form to load
in the window, Login as the window title, and passing x
and y variables for the position. Next, the code sizes
the window and saves a reference to the window instance so that the
SWF can close the window later on. You'll add login form processing
in a moment; for now, you need, at the very least, a way to close the
Login window.
The following steps add processing to the login form Cancel button:
Open the Actions panel, and enter the following code:
// Close login window
on (click) {
_root.application.login.loginInstance.deletePopUp();
}
Save your changes and test the application. You will now be able to press the Login button to pop up the login form (Figure 19); click Cancel to close the pop-up window.
Figure 19. Use the Window component
and the PopUpManager class to create intuitive pop-up
windows.