<?xml version="1.0" encoding="utf-8"?>


<!-- 
Filter Functions Example

4th June 2007

This is an example of using filter functions to filter collections of data. 
Feel free to use this code in whatever way you like.

Mrinal Wadhwa 
http://weblog.mrinalwadhwa.com

-->


<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" 
    backgroundGradientColors="[#c0c0c0, #000000]"
    creationComplete="initApp()"  viewSourceURL="srcview/index.html">
    
    <mx:Script>
        <![CDATA[
                import mx.collections.ArrayCollection;
                
                [Bindable] private var testData:ArrayCollection;
        
                private function initApp():void{
                    
                    testData = new ArrayCollection([
                        {name:"Item 1", price:18},
                        {name:"Item 2", price:50},
                        {name:"Item 3", price:77},
                        {name:"Item 4", price:52},
                        {name:"Item 5", price:43},
                        {name:"Item 6", price:97},
                        {name:"Item 7", price:56},
                        {name:"Item 8", price:81},
                        {name:"Item 9", price:63},
                        {name:"Item 10", price:29},
                        {name:"Item 11", price:84},
                        {name:"Item 12", price:31},
                        {name:"Item 13", price:78},
                        {name:"Item 14", price:48},
                        {name:"Item 15", price:23},
                    ]);
                    
                    /* set the filter function for a collection */
                    testData.filterFunction = filterForTestData;
                    
                }
                
                /* the signature of a filter function is fixed and 
                has to be like the that of this function */
                private function filterForTestData(item:Object):Boolean{
                       if(item.price < slider.value) return true;
                       else return false;      
                }
                
                /* the filter funcion is called when refresh() method is invoked on 
                 the collection */
                private function applyFilter():void{
                        testData.refresh();
                }
             
        ]]>
    </mx:Script>
    
    <mx:VBox horizontalCenter="0" top="100" horizontalAlign="center">
        <mx:Text text="Filter Functions Example" fontSize="18" color="#ffffff"/>
        <mx:HBox>
            <mx:Text text="Show items priced below:" color="#ffffff"/>
            <mx:HSlider id="slider" liveDragging="true" change="applyFilter()"
                minimum="0" maximum="100" value="100"
                snapInterval="1" borderColor="#ffffff"/>
            <mx:Label text="Rs.{slider.value}" color="#ffffff"/>
        </mx:HBox>
        <mx:DataGrid dataProvider="{testData}" 
            width="100%" height="350">
            <mx:columns>
               <mx:DataGridColumn headerText="Name" dataField="name"/>
               <mx:DataGridColumn headerText="Price(Rs)" dataField="price"/>
            </mx:columns>
         </mx:DataGrid>
    </mx:VBox>
    
    
</mx:Application>