<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" xmlns:collections="eu.rotundu.collections.*"
    creationComplete="creationCompleteHandler();" viewSourceURL="srcview/index.html">
<mx:Script>
    <![CDATA[
        
        private var selectedMinPrice:int = 0;
        private var selectedMaxPrice:int = 3000;
        private var selectedType:String = "all";
        private var selectedCondition:String = "all";
        private var selectedVendor:String = "all";
        
        
        private function creationCompleteHandler():void
        {
            /* setup the filters array: in order to take effect
             * you must call refresh() on the collection
             */
            productsCollection.filterFunctions = 
                [
                    filterByPrice, filterByType,
                    filterByCondition, filterByVendor
                ]
            productsCollection.refresh();
        }
        
        // your classic filter functions
        private function filterByPrice( item:Object ):Boolean
        {
            if( selectedMinPrice <= item.productPrice && selectedMaxPrice > item.productPrice )
                return true;
            return false;
        }
        
        private function filterByType( item:Object ):Boolean
        {
            if( item.productType == selectedType || selectedType == "all" )
                return true;
            return false;
        }
        
        private function filterByCondition( item:Object ):Boolean
        {
            if( item.productCondition == selectedCondition || selectedCondition == "all" )
                return true;
            return false;
        }
        
        private function filterByVendor( item:Object ):Boolean
        {
            if( item.productVendor == selectedVendor || selectedVendor == "all" )
                return true;
            return false;
        }
        
        // change handlers, nothing new here
        private function productPriceChangeHandler():void
        {
            selectedMinPrice = hsProductPrice.values[0];
            selectedMaxPrice = hsProductPrice.values[1];
            productsCollection.refresh();
        }
        
        private function productTypeChangeHandler():void
        {
            if( cbProductType.selectedItem != null )
                selectedType = cbProductType.selectedItem.data;
            productsCollection.refresh();
        }
        
        private function productConditionChangeHandler():void
        {
            if( cbProductCondition.selectedItem != null )
                selectedCondition = cbProductCondition.selectedItem.data;
            productsCollection.refresh();
        }
        
        private function productVendorChangeHandler():void
        {
            if( cbProductVendor.selectedItem != null )
                selectedVendor = cbProductVendor.selectedItem.data;
            productsCollection.refresh();
        }
    ]]>
</mx:Script>
    <mx:DataGrid horizontalCenter="0" height="250" y="89">
        
        <mx:dataProvider>
            <collections:ArrayCollectionExtended id="productsCollection">
                <mx:Array>
                    <mx:Object productName="Solar Panel" productPrice="1500" productType="home" productCondition="new" productVendor="SunPan" />
                    <mx:Object productName="Laptop Battery" productPrice="80" productType="computers" productCondition="used" productVendor="Laptop INC." />
                    <mx:Object productName="Laptop Screen" productPrice="300" productType="computers" productCondition="new" productVendor="Laptop INC." />
                    <mx:Object productName="Temp. Monitor" productPrice="95" productType="home" productCondition="new" productVendor="YourHome LTD" />
                    <mx:Object productName="Motion Sensor" productPrice="2500" productType="home" productCondition="new" productVendor="Secu-Dome" />
                    <mx:Object productName="Brake Disk" productPrice="200" productType="auto" productCondition="used" productVendor="Voltran" />
                    <mx:Object productName="Safety Belt" productPrice="500" productType="auto" productCondition="new" productVendor="PanAuto West" />
                    <mx:Object productName="Multimeter" productPrice="40" productType="electro" productCondition="new" productVendor="VOID" />
                    <mx:Object productName="Capacitor" productPrice="5" productType="electro" productCondition="new" productVendor="VOID" />
                </mx:Array>
            </collections:ArrayCollectionExtended>
        </mx:dataProvider>        
        
        <mx:columns>
            <mx:DataGridColumn dataField="productName" headerText="Name" />
            <mx:DataGridColumn dataField="productPrice" headerText="Price" />
            <mx:DataGridColumn dataField="productType" headerText="Type" />
            <mx:DataGridColumn dataField="productCondition" headerText="Condition" />
            <mx:DataGridColumn dataField="productVendor" headerText="Vendor" />
        </mx:columns>
        
    </mx:DataGrid>
    <mx:HBox y="365" width="700" horizontalCenter="0" horizontalAlign="center" verticalAlign="middle">
        <mx:HSlider id="hsProductPrice" allowTrackClick="true" minimum="0" maximum="3000" snapInterval="10" liveDragging="true" thumbCount="2" toolTip="Price" width="220" 
            change="productPriceChangeHandler()" values="[0,3000]" labels="[0, 3000]"/>
        
        <mx:ComboBox id="cbProductType" change="productTypeChangeHandler();">
            <mx:dataProvider>
                <mx:Array>
                    <mx:Object label="-- All Types --" data="all" />
                    <mx:Object label="home" data="home" />
                    <mx:Object label="computers" data="computers" />
                    <mx:Object label="auto" data="auto" />
                    <mx:Object label="electro" data="electro" />
                </mx:Array>
            </mx:dataProvider>
        </mx:ComboBox>
        
        <mx:ComboBox id="cbProductCondition" change="productConditionChangeHandler();">
            <mx:dataProvider>
                <mx:Array>
                    <mx:Object label="-- All Conditions --" data="all" />
                    <mx:Object label="new" data="new" />
                    <mx:Object label="used" data="used" />
                </mx:Array>
            </mx:dataProvider>
        </mx:ComboBox>
        <mx:ComboBox id="cbProductVendor" change="productVendorChangeHandler();">
            <mx:dataProvider>
                <mx:Array>
                    <mx:Object label="-- All Vendors --" data="all" />
                    <mx:Object label="SunPan" data="SunPan" />
                    <mx:Object label="Laptop INC." data="Laptop INC." />
                    <mx:Object label="YourHome LTD" data="YourHome LTD" />
                    <mx:Object label="Secu-Dome" data="Secu-Dome" />
                    <mx:Object label="Voltran" data="Voltran" />
                    <mx:Object label="PanAuto West" data="PanAuto West" />
                    <mx:Object label="VOID" data="VOID" />
                </mx:Array>
            </mx:dataProvider>
        </mx:ComboBox>
    </mx:HBox>
</mx:Application>