필터 기능이있는 컬렉션에서 항목을 제거한 다음 필터 기준을 충족하지 않는 새 항목을 추가하려면 어떻게해야합니까?
-
21-08-2019 - |
문제
컬렉션이 있고 특정 속성이 True로 설정된 모든 항목을 제거하고 싶습니다. 나는 이것을 달성하기 위해 필터 기능을 사용합니다. 내 질문은 해당 속성이 True로 설정된 컬렉션에 새 항목을 어떻게 추가 할 수 있습니까? 필터 기능이 여전히 적용되고 항목이 추가되지 않았습니다 ....
전체 컬렉션을 반복하고 한 번에 하나씩 항목을 제거해야합니까? 나는 그것이 정확히 새로 고침 ()가하는 일이라고 생각했다.
감사.
<?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 속성에 모든 항목을 여전히 볼 수 있습니다.
필터 기능이 여전히 적용되는 동안 새 항목을 추가하면 필터링이 적용됩니다.
목록에서 항목을 영구적으로 제거하려면 배열로 변환하고 사용하십시오. Array#filter
.
var newCollection:ArrayCollection =
new ArrayCollection(oldCollection.toArray().filter(myFilterFunction))
제휴하지 않습니다 StackOverflow