如何从具有过滤功能的集合中删除项目,然后添加不符合过滤条件的新项目?

时间:2021-09-02 23:18:32

I have a Collection, and I want to remove all items that have a certain property set to true. I use a filterFunction to accomplish this. My question is, how can I add new items to the Collection that have that property set to true? The filterFunction is still applied, and the item is not added....

我有一个Collection,我想删除所有属性设置为true的项目。我使用filterFunction来完成此任务。我的问题是,如何向Collection中添加具有该属性设置为true的新项? filterFunction仍然应用,并且没有添加项目....

Do I have to iterate through the entire collection and remove items one at a time? I thought that that is exactly what refresh() does.

我是否必须遍历整个集合并一次删除一个项目?我认为这正是refresh()的作用。

Thanks.

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical">
    <mx:Script>
        private function hideSpecialItems():void
        {
            items.filterFunction = 
                function (item:Object):Boolean
                {
                    return item.isSpecial;
                }

            items.refresh();

            trace(items.length.toString()); // 2
        }

        private function addSpecialItem():void
        {
            items.addItem({name: "new Special Item", isSpecial: true});

            trace(items.length.toString()); // Item is added - returns 3
        }

        private function addNormalItem():void
        {
            items.addItem({name: "new Item", isSpecial: false});

            trace(items.length.toString()); // Item not added - returns 2
        }
    </mx:Script>

    <mx:ApplicationControlBar>
        <mx:Button label="Hide Items That Aren't Special" click="hideSpecialItems();" />

        <mx:Button label="Add a Normal Item" click="addNormalItem();" />

        <mx:Button label="Add a Special Item" click="addSpecialItem();" />
    </mx:ApplicationControlBar>

    <mx:ArrayCollection id="items">
        <mx:Array>
            <mx:Object name="item 1" isSpecial="{false}" />
            <mx:Object name="item 2" isSpecial="{false}" />
            <mx:Object name="item 3" isSpecial="{false}" />
            <mx:Object name="item 4" isSpecial="{true}" />
            <mx:Object name="item 5" isSpecial="{true}" />
            <mx:Object name="item 6" isSpecial="{false}" />
        </mx:Array>
    </mx:ArrayCollection>

    <mx:DataGrid dataProvider="{items}" />
</mx:Application>

1 个解决方案

#1


filterFunction doesn'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.

filterFunction实际上并不从ArrayCollection中删除项目。它只是从视图中隐藏它们。您仍然可以在ArrayCollection.source属性中看到所有项目。

If you add new items while filterFunction is still applied, they too are subject to filtering.

如果在仍然应用filterFunction时添加新项目,则它们也会受到过滤。

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

要从列表中永久删除项目,请将其转换为数组并使用Array#filter。

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

#1


filterFunction doesn'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.

filterFunction实际上并不从ArrayCollection中删除项目。它只是从视图中隐藏它们。您仍然可以在ArrayCollection.source属性中看到所有项目。

If you add new items while filterFunction is still applied, they too are subject to filtering.

如果在仍然应用filterFunction时添加新项目,则它们也会受到过滤。

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

要从列表中永久删除项目,请将其转换为数组并使用Array#filter。

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