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?
 
jimmym715Connect With a Mentor Commented:
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
 
Siva Prasanna KumarConnect With a Mentor Principal 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
 
aduvallAuthor Commented:
Thank You!!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.