我怎样才能从过滤功能的集合中删除的项目,然后添加不符合过滤条件的新项目?
-
21-08-2019 - |
题
我有一个集合,我想删除具有一定的属性设置为true的所有项目。我使用的filterFunction做到这一点。我的问题是,我怎么可以添加新的项目,已经该属性设置为true的收藏?所述的filterFunction仍在应用,并且不被添加的项目...
我要通过整个集合遍历和删除项目一次一个?我认为,这正是刷新()一样。
感谢。
<?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>
解决方案
filterFunction
实际上不除去从ArrayCollection的项目。它只是隐藏了他们的看法。你仍然可以看到ArrayCollection.source财产的所有物品。
如果您添加新的项目,而仍然采用的filterFunction,他们也受到过滤。
要永久地从列表中删除项目,将其转换为阵列和使用Array#filter
。
var newCollection:ArrayCollection =
new ArrayCollection(oldCollection.toArray().filter(myFilterFunction))
不隶属于 StackOverflow