User Guide
32 Creating an Application with Components (Flash Professional Only)
Add an event listener to filter the gift ideas
In this section, you add an event listener to detect when a user selects an offense in the What
Did You Do? section (the
problems_cb ComboBox instance). The listener includes a
function that filters the Gift Ideas list according to the offense the user chooses. Selecting a
minor offense displays a list of modest gifts (such as a CD or flowers); selecting a more serious
offense displays more opulent gifts.
For more information on working with event listeners, see “Using event listeners” in Learning
ActionScript 2.0 in Flash.
■ In the Actions panel, add the following code:
/* Define a listener for the problems_cb ComboBox instance.
This listener will filter the products in the DataSet (and DataGrid).
Filtering is based on the severity of the currently selected item in the
ComboBox. */
var cbListener:Object = new Object();
cbListener.change = function(evt:Object) {
products_ds.filtered = false;
products_ds.filtered = true;
products_ds.filterFunc = function(item:Object) {
// If the current item's severity is greater than or equal to the
// selected item in the ComboBox, return true.
return (item.severity>=evt.target.selectedItem.severity);
};
};
// Add the listener to the ComboBox.
problems_cb.addEventListener("change", cbListener);
Resetting the filtered property (setting it to false and then to true) at the beginning of
the
change() function ensures that the function will work properly if the user changes the
What Did You Do? selection repeatedly.
The
filterFunc() function checks whether a given item in the array of gifts falls within the
severity the user selected in the combo box. If the gift is within the selected severity range, it is
displayed in the DataGrid instance (which is bound to the DataSet instance).
The last line of code registers the listener to the
problems_cb ComboBox instance.