Thanks for all of the help guys! I have gotten things pretty much
working now: I just need to sort out some issues relating to working
with multidimensional data-sources, which I'm going to post in another
thread. In case it is helpful to anyone else though, here is the
near-completed class:

/***********************************************************************\
********
  * Filter Manager
 
************************************************************************\
******/
package custom {

     import mx.containers.Form;
     import mx.containers.FormItem;
     import mx.controls.Alert;
     import mx.collections.ArrayCollection;
     import mx.controls.DataGrid;
     import mx.controls.dataGridClasses.DataGridColumn;
     import mx.controls.TextInput;
     import mx.utils.ObjectUtil;
     import flash.events.Event;

    
/***********************************************************************\
****
     * Class definition for a class to manage the various search filters
    
************************************************************************\
***/
     public class FilterManager extends Form {

         private var _datasource:ArrayCollection;
         private var _datagrid:DataGrid;
         private var _filterSettings:Array;

        
/***********************************************************************
         * FilterManager (Constructor)
         *
         * Creates an instance of the filter manager class
        
***********************************************************************/
         public function FilterManager():void {
             super();
         }


        
/***********************************************************************
         * datagrid
         *
         * A reference to the DataGrid object for which the filter will
be applied
        
***********************************************************************/
         [Bindable]
         public function set datagrid(dg:DataGrid):void {
             _datagrid = dg;
             this.initFilters(); // DG not yet ready in constructor...
             this.setupUI();
         }

         public function get datagrid():DataGrid {
             return _datagrid;
         }

        
/***********************************************************************
         * datasource
         *
         * A reference to the actual data to be filtered
        
***********************************************************************/
         [Bindable]
         public function set datasource(data:ArrayCollection):void {
             _datasource = data;
         }

         public function get datasource():ArrayCollection {
             return _datasource;
         }

        
/***********************************************************************
         * initFilters
         *
         * Builds an array to manage the different filters that may be
set
        
***********************************************************************/
         private function initFilters():void {
             this._filterSettings = new Array();

             // Create an array for each column to keep track of it's
filter status
             for each (var col:DataGridColumn in _datagrid.columns) {
                 var d:Object = new Object();

                 // Add all filterable items (exclude thumbnails, etc)
                 if (col['dataField'] != null) {
                     d.field   = col['dataField'];
                     d.header  = col['headerText'];
                     d.enabled = false;
                     d.filter  = null;
                     this._filterSettings.push(d);
                 }
             }
         }

        
/***********************************************************************
         * setupUI
         *
         * Sets up the form fields for entering filters
        
***********************************************************************/
         private function setupUI():void {
             for each (var item:Object in this._filterSettings) {
                 var f:FormItem = new FormItem();
                 f.label = item.header;

                 // Filter input field
                 var input:TextInput = new TextInput();

                 // Setup event-handler
                 var self:FilterManager = this;
                 input.addEventListener("change", function (e:Event):void
{
                     item.filter = e.target.text;

                     // Update filter information
                     item.enabled = (item.filter == "" ? false : true);

                     // Update filter on DataGrid and throw event
                     self._datasource.filterFunction = self.filter;
                     self._datasource.refresh();
                 });

                 // Add text input to FormItem
                 f.addChild(input);

                 // Save a reference to the form control
                 item.ui = f;
                 addChild(f);
             }
         }

        
/***********************************************************************
         * filter
         *
         * Filter function created based on FilterManager settings
        
***********************************************************************/
         public function filter(item:Object):Boolean {
             for each (var filterField:Object in this._filterSettings) {
                 if (filterField.enabled) {
                    
if(String(item[filterField.field]).toLowerCase().search(filterField.filt\
er.toLowerCase()) == -1)
                         return false;
                 }
             }

             return true;
         }

        
/***********************************************************************
         * component overrides
        
***********************************************************************/
         override protected function createChildren():void {
             super.createChildren();
         }

         override protected function commitProperties():void {
             super.commitProperties();
         }

         override protected function measure():void {
             super.measure();
         }

         override protected function
updateDisplayList(unscaledWidth:Number,
                                                
unscaledHeight:Number):void {
             super.updateDisplayList(unscaledWidth,unscaledHeight);
         }

     }
}

And to instantiate the filter manager in MXML:

<custom:FilterManager datagrid="{Application.application.myDataGrid}"
datasource="{Application.application.myDataGrid.myDataProvider}"
width="100%" height="100%"/>

I'll post an update once I get the issues sorted out with the
multidimensioal data. Because I wanted to use some information that is
already stored in the DataGrid columns (headers and associated
datafields), I decided to have the FilterManager accept a reference to a
DataGrid object. The actual data provider is also passed in so that the
filter can be applied simply.

I am open to any feedback or suggestions people may have: this is the
first custom component I've written in pure AS, so I'm sure there is
lots of room for improvement.

Thanks again Steve and Erick for all of the feedback and assistance :)
Keith

Reply via email to