DataSet.filterFunc

Availability

Flash Player 7.

Edition

Flash MX Professional 2004.

Usage

dataSetInstance.filterFunc = function (item:Object):Boolean {
    // return true|false;
};

Description

Property; specifies a function that determines which items are included in the current view of the collection. When DataSet.filtered is set to true, the function assigned to this property is called for each record (transfer object) in the collection. For each item that is passed to the function, it should return true if the item should be included in the current view, or false if the item should not be included in the current view.

When changing the filter function on a data set, you must set the filtered property to false and then true again in order for the proper modelChanged event to be generated. Changing the filterFunc property won't generate the event.

Also, if a filter is already in place when the data loads in (modelChanged or updateAll), the filter isn't applied until filtered is set to false and then back to true again.

Example

In the following example, filtering is enabled on the DataSet object named employee_ds. The specified filter function returns true if the empType field in each item is set to "management"; otherwise, it returns false.

employee_ds.filtered = true;
employee_ds.filterFunc = function (item:Object):Boolean {
    // Filter out employees who are managers. 
    return(item.empType != "management");
};

In the following example, you filter the contents of a DataSet component based on the selected item in a ComboBox component. The ComboBox component lets you select all people, males only, or females only.

Drag the a DataSet, DataGrid, and ComboBox components to the Stage, and give them instance names of my_ds, data_dg, and filter_cb respectively. Select the my_ds DataSet instance on the Stage and add two new properties: name and gender. Create a data binding between the dataProvider property of the DataSet and the dataProvider of the DataGrid component by using the Bindings panel of the Component inspector. Add the following ActionScript code to Frame 1 of the main timeline:

my_ds.dataProvider = new Array({name:"Charles", gender:"M"}, {name:"Buddy", gender:"M"}, {name:"Walter", gender:"M"}, {name:"Ellen", gender:"F"}, {name:"Jamie", gender:"F"}, {name:"Sarah", gender:"F"}, {name:"Adam", gender:"M"});
my_ds.addSort("nameSort", ["name"]);
my_ds.addSort("genderSort", ["gender", "name"]);
my_ds.useSort("genderSort");

filter_cb.dataProvider = [{label:"All", value:""} , {label:"Male only", value:"M"}, {label:"Female only", value:"F"}];
filter_cb.addEventListener("change", filterListener);
function filterListener(evt_obj:Object):Void {
    var selItem:Object = evt_obj.target.selectedItem;
    if (selItem.value.length == 0) {
        my_ds.filtered = false;
    } else {
        my_ds.filtered = true;
        my_ds.filterFunc = function(item:Object):Boolean {
            return (item.gender == selItem.value);
        }
    }
}

The following example populates a DataGrid component from content dynamically loaded using the XMLConnector component. When a user clicks a CheckBox instance on the Stage, the contents of the DataSet component are filtered and are updated automatically in the DataGrid component.

Drag the following components to the Stage, and give them the following instance names:

Download a copy of the following XML document, and save it to your local hard disk: www.helpexamples.com/flash/xml/reviews.xml. This XML document loads dynamically using the XMLConnector component,. You'll use the local copy to import the XML schema into the DataSet component. Select the XMLConnector instance on the Stage, and select the Schema tab from the Component inspector. Select the results property, and click Import a Schema From a Sample XML File. Select the XML document that you downloaded to your local hard disk, and click Open. The schema of the XML document should import into the DataSet component. With the reviews_xmlconn XMLConnector instance still selected on the Stage, add a binding from the reviews_xmlconn.results.reviews.review array to the dataProvider property of the reviews_ds DataSet instance. Set the direction of the data binding to Out in the Bindings tab. Select the reviews_ds DataSet instance on the Stage, and add another binding for the dataProvider property. With the reviews_ds instance selected, select the Bindings tab of the Component inspector. Click the Add Binding button and add a binding from the dataProvider property of the DataSet component to the dataProvider property of the reviews_dg DataGrid instance.

Add the following code to Frame 1 of the main timeline:

editorsChoice_ch.label = "Editor's Choice";

reviews_xmlconn.direction = "receive";
reviews_xmlconn.multipleSimultaneousAllowed = false;
reviews_xmlconn.URL = "http://www.helpexamples.com/flash/xml/reviews.xml";
reviews_xmlconn.trigger();

reviews_dg.setSize(320, 240);
reviews_dg.addColumn("name");
reviews_dg.addColumn("rating");
reviews_dg.addColumn("reviewDate");
reviews_dg.getColumnAt(0).width = 100;
reviews_dg.getColumnAt(1).width = 100;
reviews_dg.getColumnAt(2).width = 100;
reviews_dg.setStyle("alternatingRowColors", [0xFFFFFF, 0xF6F6F6]);

editorsChoice_ch.addEventListener("click", editorsChoiceListener);
function editorsChoiceListener(evt_obj:Object):Void {
    reviews_ds.filtered = evt_obj.target.selected;
    reviews_ds.filterFunc = function(item:Object):Boolean {
        return (item.editorsChoice == 1);
    };
}

Select Control > Test Movie. By default, the DataGrid component should display each of the reviews from the external XML document. Clicking on the Editor's Choice CheckBox component causes the DataSet component to be filtered, so that only the specific reviews flagged as an editor's choice appear.

See also

DataSet.filtered


Flash CS3