1 Reply Latest reply on Dec 27, 2010 4:41 PM by saisri2k2

    Filtering DG with ComboBox ?

    Whitetimer

      Hi All

       

      How do i filter a dataGrid using the selectedItem of a comboBox ?

       

      I have a comboBox which shows categories from an xml file like this

       

      <categories>
          <category catID="1" catName="Category One" />
          <category catID="2" catName="Category Two" />
          <category catID="3" catName="Category Three" />
          <category catID="4" catName="Category Four" />
      </categories>
      

       

      And i have a dataGrid which loads products from an xml like this

       

      <categories>
           <category catID="1" catName="Whitetime Healing">
                <product id='1001' catID='1' name='' description='' image='' price='25' />
                <product id='1002' catID='1' name='' description='' image='' price='40' />
                <product id='1003' catID='1' name='' description='' image='' price='' />
                <product id='1004' catID='1' name='' description='' image='' price='' />
           </category>
           <category catID="2" catName="Gemstone Layouts">
                <product id='2001' catID='2' name='' description='' image='' price='25' />
                <product id='2002' catID='2' name='' description='' image='' price='40' />
                <product id='2003' catID='2' name='' description='' image='' price='' />
                <product id='2004' catID='2' name='' description='' image='' price='' />
           </category>
           <category catID="3" catName="Classes & Courses">
                <product id='3001' catID='3' name='' description='' image='' price='25' />
                <product id='3002' catID='3' name='' description='' image='' price='40' />
                <product id='3003' catID='3' name='' description='' image='' price='' />
                <product id='3004' catID='3' name='' description='' image='' price='' />
           </category>
           <category catID="4" catName="Other">
                <product id='4001' catID='4' name='' description='' image='' price='25' />
                <product id='4002' catID='4' name='' description='' image='' price='40' />
                <product id='4003' catID='4' name='' description='' image='' price='' />
                <product id='4004' catID='4' name='' description='' image='' price='' />
           </category>
      </categories>
      

       

       

      I am wanting to have the dataGrid show all items or a list based on the category selected in the comboBox ...

       

      Many Thanks

        • 1. Re: Filtering DG with ComboBox ?
          saisri2k2 Level 4

          on getting the data do the followng

           

          [Bindable]
                         private var xList:XMLList;
          
          protected function button1_clickHandler(event:MouseEvent=null):void
                         {
                              xList = new XMLList();
                              for each(var obj:XML in cat.category){
                                   for each(var child:XML in obj.product)
                                        xList += child;
                              }
                              trace(xList.toString())
                         }
          
          
          

           

          The xList will be like following after you do the above logic. this you can apply to the data grid as is. and for filter you can do the following

          <product id="1001" catID="1" name="" description="" image="" price="25"/>
          <product id="1002" catID="1" name="" description="" image="" price="40"/>
          <product id="1003" catID="1" name="" description="" image="" price=""/>
          <product id="1004" catID="1" name="" description="" image="" price=""/>
          <product id="2001" catID="2" name="" description="" image="" price="25"/>
          <product id="2002" catID="2" name="" description="" image="" price="40"/>
          <product id="2003" catID="2" name="" description="" image="" price=""/>
          <product id="2004" catID="2" name="" description="" image="" price=""/>
          <product id="3001" catID="3" name="" description="" image="" price="25"/>
          <product id="3002" catID="3" name="" description="" image="" price="40"/>
          <product id="3003" catID="3" name="" description="" image="" price=""/>
          <product id="3004" catID="3" name="" description="" image="" price=""/>
          <product id="4001" catID="4" name="" description="" image="" price="25"/>
          <product id="4002" catID="4" name="" description="" image="" price="40"/>
          <product id="4003" catID="4" name="" description="" image="" price=""/>
          <product id="4004" catID="4" name="" description="" image="" price=""/>
          
           
          

           

           

          filter function (not tested. should work for you)

               private function filterFirst(item:Object):Boolean
                         {
                              return item.@catID == combobox.selectedItem.@catID
                         }
          

           

          for filter function http://cookbooks.adobe.com/post_Using_the_to_ArrayCollection_s_filterFunction-5441.html