Delete filtered items in arraycollection.

How do I delete items that have been filtered in an array collection.  I want to do this because when items are filtered they are not truly removed from the array-collection.  So if I add items that are the same as the items filtered in the array collection it will just add on to the previously filtered items.

 private function filterManagersb(item:Object):Boolean {  
                 
                 return item.item != "someitem";
          }

if (items.getItemAt(i).item == "someitem"){
         items.filterFunction = filterManagersb;
   // need filtered items to be deleted.
}

public function addItem():void {
                     mx.core.Application.application.items.filterFunction = null;
                 mx.core.Application.application.items.refresh();
                 //code to add some item will be here
}
aduvallAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Siva Prasanna KumarPrincipal Solutions ArchitectCommented:
filterFunction won't actually remove the items from the ArrayCollection. It just hides them from the view. You can still see all the items in ArrayCollection.source property, as seen adding new items while filterFunction is still applied, they too are subject to filtering.

To permanently remove items from a list, convert it to an Array and use Array#filter.

var newCollection:ArrayCollection =
    new ArrayCollection(oldCollection.toArray().filter(myFilterFunction))

But why do you want to remove something which is not visible. if thats the case I will not use filter function instead will use removeItemIt and remove it from the source.




Open in new window

0
aduvallAuthor Commented:
Thanks I will check it out.   For the app. I am building a person may add the items again.  When this happens and I clear the filter it will show the previously filtered items plus the new items since the filter has been cleared.  If I delete the filtered items when I add new I should not have that issue.  I hope.
0
Siva Prasanna KumarPrincipal Solutions ArchitectCommented:
infact it will be better if you retain the actual array collection as is and get the filtered array collection as the source for views (combox or what ever) instead of adding the element again.
0
jimmym715Commented:
Allow me to offer two pieces of advice here:

1.  Use a HashMap (http://code.ericfeminella.com/classes/as3/HashMap.as.html) or similar to ensure that you only have one of each item in your collection based on whatever the key is.

2. As the previous commenter noted above, the ArrayCollection.source returns the  data Array for that ArrayCollection.  Given that, If you run into a case where you actually do need to remove items from an Array, you may be able to use splice:

    for(var i:int = 0; i < itemArray.length; i++)
       {
        if(itemArray[i]  [meets condition to remove item])
        {
            itemArray.splice(i, 1);
            i--;
        }
    }

http://www.adobe.com/livedocs/flex/3/langref/Array.html#splice()

NOTE: splice also allows items to be added to an array, too, which can be handy as a replace function in some cases

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
aduvallAuthor Commented:
Thank You!!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Adobe Flash

From novice to tech pro — start learning today.