views:

1436

answers:

4

Hi all,

I'm having some issues when calling getItemIndex on an ArrayCollection with a filterFunction set.

I do something like myAC.removeItemAt(myAC.getItemIndex(myObject)), which works fine when the filtering hasn't been applied. As soon as filtering is applied, getItemIndex seems to return -1 in every case.

Has anyone come across this before? What the best way to remove an item form a filtered ArrayCollection?

Thanks a lot.

Evan

A: 

Any time I've dealt with adding and removing items from ArrayCollections in Flex, I've always kept a copy of the original ArrayCollection. Any adding or removing of items happen to that original copy.

Once the changes have been made to the original, I move those forward to the filtered list.

Justin Niessner
A: 

What exactly is your filter filtering out? If you've filtered out everything, getItemIndex should return -1.

Are you hoping to remove items that are still visible when your filter has been applied? If you still want to remove an item that's filtered out, you could temporarily disable the filter:

var filter:Function = ac.filterFunction;
ac.fiterFunction = null;
ac.refresh();

// remove item

ac.filterFunction = filter;
ac.refresh();
Stiggler
A: 

Yeah, so I did find out that I was changing the property of the object - to one that would have it filtered out - prior to trying to remove it. Of course I would get -1 in that case. My mistake.

Ended up going with your suggestion, Stiggler. Seems to work fine, though it seems like there should be a less hackish way to handle this type of thing. Perhaps a parameter you could pass to removeItemAt that would let you access the unfiltered collection.

Anyway, thanks to both of you for your responses. Much appreciated.

Regards,

Evan

evanmcd
+2  A: 

I think you'll find there is a source object within the ArrayCollection. What you are seeing is a view of the underlying data with a sort or filter applied. You really want to delete from the underlying source object.