2 Replies Latest reply on Jun 6, 2010 9:27 PM by SiHoop

    Subset of an arrayCollection

    SiHoop Level 1

      How do I select a subset of an array

      Collection to appear in a datagrid? For example, in the code below, how do I select only the records where pop > 2000?

       

      <?xml version="1.0"?>
      <!-- dpcontrols\SimpleDP.mxml -->
      <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"  creationComplete="creationCompleteHandler()">
          <mx:Script>
              <![CDATA[
      import mx.collections.ArrayCollection;
      [Bindable] private var _populations:ArrayCollection;
                  public function creationCompleteHandler():void {
                      _populations=new ArrayCollection;
                      _populations.addItem({country: "Canada", continent:"NA", pop:"50"});
                      _populations.addItem({country: "Ven", continent:"SA", pop:"1000"});
                      _populations.addItem({country: "USA", continent:"NA", pop:"5000"});
                      _populations.addItem({country: "Brazil", continent:"SA", pop:"4000"});
                   }
               ]]>
          </mx:Script>
          <mx:DataGrid  id="datagrid"   dataProvider="{_populations}" width="100%" height="100%" />
             
      </mx:Application>

        • 1. Re: Subset of an arrayCollection
          David_F57 Level 5

          hi,

           

           

          You use a filter function on your array collection, this is reasonably simple I have a basic example which I did for another poster a few weeks back

           

          http://flashhub.net/filter/ - source is included

           

           

          So your filter would look something like this, you could hard code the value in but it is always better to use variables so that any part of your program can alter the filter requirements.

           

                                  private var minPopulation: int = 500;  <=== global variable

           

                      protected function arrColl_filterFunc(item:Object):Boolean
                      {
                              if (item.pop > minPopulation)                
                              { 
                                  return true     
                              }
                              else
                                  return false;
                      }
          

          • 2. Re: Subset of an arrayCollection
            SiHoop Level 1

            Nice-- thanks.