3 Replies Latest reply on Aug 31, 2007 3:36 PM by mrharb007

    Filter top 5 on ArrayCollection?

    mrharb007
      Hi Guys,

      Would someone mind telling me how I could filter an ArrayCollection to return only the first 5 items?

      Thanks,
      James
        • 1. Re: Filter top 5 on ArrayCollection?
          mrharb007 Level 1
          Nobody has any idea how to accomplish this? Why is this so hard... I feel like nobone here understands flex outside of cookie-cutter applications. Its frustrating to have to dig through source code every time one want to do something simple but not supported out of the box.

          -James
          • 2. Re: Filter top 5 on ArrayCollection?
            Bob Saggot Level 1
            var i:Number;
            var j:Number;
            var tempArrayCollection:ArrayCollection = new ArrayCollection();

            if(arrayCollectionName.length < 5)
            j = arrayCollectionName.length
            else
            j = 5

            for(i = 0; i < j; i++)
            tempArrayCollection.addItem(arrayCollectionName.getItemAt(i);

            • 3. Re: Filter top 5 on ArrayCollection?
              mrharb007 Level 1
              Thanks Saggot. I probably should have said that I was looking for an encapsulated solution because my application was complex (multiple ArrayCollections feeding on a single source).

              Anyway heres my solution

              package
              {
              include "imports.as"

              public class Top5ArrayCollection extends ArrayCollection
              {
              public override function dispatchEvent(event:Event):Boolean
              {
              if(event.type == CollectionEvent.COLLECTION_CHANGE)
              {
              // localIndex will be null when no sort or filter
              localIndex = localIndex ?
              localIndex.slice(0, 5) :
              source.slice(0, 5);
              }

              return super.dispatchEvent(event);
              }
              }
              }

              -James