4 Replies Latest reply on Apr 27, 2011 8:18 AM by mattcom

    ArrayCollection filterFunction

    mattcom Level 1

      Does anyone know how to make a copy of the ArrayCollection as an instance of the filtered one.

       

      For example if I have myArrayCollection and I filter it with a filterFunction and say have 100 returned results

       

      myArrayCollection.filterFunction = myFilterFunction;

      myArrayCollection.refresh();

      trace(myArrayCollection.length.toString()) // 100;

       

      but if I do this:

       

      myACcopy = myArrayCollection

      myACcopy.filterFunction = myFilterFunction2;

      myACcopy.refresh();

       

      trace(myACcopy.length.toString()) // 15

      trace(myArrayCollection.length.toString()) // 15;

       

       

      You see, when I filter the copy of the AC and refresh it, somehow it replaces the values of myArrayCollection, and I don't want that.  I want myArrayCollection to still have 100 results.

       

       

      Any suggestions?

       

                private function countNode2(field:String, value:String):int
                {
                      var tempArr:ArrayCollection = new ArrayCollection();
                      tempArr = HMData;
                      myFilterField = field;
                      myFilterValue = value;
                      tempArr.filterFunction = filterCount;
                      tempArr.refresh();
                     
                      return tempArr.length;
                  }

        • 1. Re: ArrayCollection filterFunction
          saisri2k2 Level 4

          http://jodieorourke.com/view.php?id=105&blog=news

           

          you are not cloning the array collection, From what I see, you are playing with the same reference of the array collection.

          1 person found this helpful
          • 2. Re: ArrayCollection filterFunction
            ScalaEnthusiast

            The filterfunction just creates a filtered view of your collection - it doesn't actually remove anything. If you want to see the full collection again, you need to set the filterfunction to null and call refresh() on the collection.

             

            If you need a totally separate identical collection to modify, see the post referenced in the other answer to your question. Doing the copy could get expensive so make sure that is what you actually "need" to do and not just filter the view of the one collection.

            1 person found this helpful
            • 3. Re: ArrayCollection filterFunction
              mattcom Level 1

              Thank you for your answers, unfortunately I've tried the suggested way through the link

               

              original HMData.length = 500

              HMData.filterFunction  = ...

              HMData.refresh()

               

              filtered HMData.length = 200

               

               

              var tempArr:ArrayCollection = new ArrayCollection(HMData.source);

              tempArr.length = 500;

               

              unfortunately it ignores the previous filtering on HMData.

               

              I also tried

              var tempArr:ArrayCollection = ObjectUtil.copy(HMData as Object) as ArrayCollection;

               

              tempArr.length = 500 there as well.

              • 4. Re: ArrayCollection filterFunction
                mattcom Level 1

                Ok, I figured out the answer. It's:

                 

                var tempArr:ArrayCollection = new ArrayCollection();
                tempArr.source = HMData.toArray();

                 

                 

                now after I run the filter on tempArr it's length differs from HMData as desired.

                 

                 

                          private function countNode2(field:String, value:String):int
                            {
                                var tempArr:ArrayCollection = new ArrayCollection();
                                tempArr.source = HMData.toArray();
                                myFilterField = field;
                                myFilterValue = value;
                                tempArr.filterFunction = filterCount;
                                tempArr.refresh();
                               
                                return tempArr.length;
                            }
                           
                            private function filterCount(item:Object):Boolean
                            {
                                if(item[myFilterField] == myFilterValue)
                                {
                                    return true;
                                } else {
                                    return false;
                                }
                            }