In case you didn't know you can have multiple ArrayCollection with the same array as source. When you add to any of the collections or to the array, all the other collections will contain the new item. However, if you use databinding on the collection the events are not triggered when you add the item to another collection. Because of that you need to manually refresh each ArrayCollection.
You can see a demo here. Bellow you have the source code.
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
layout="vertical"
creationComplete="onCreationComplete()">
<mx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
public var theArray:Array = new Array();
[Bindable]
public var maleCollection:ArrayCollection = new ArrayCollection(theArray);
[Bindable]
public var femaleCollection:ArrayCollection = new ArrayCollection(theArray);
[Bindable]
public var allCollection:ArrayCollection = new ArrayCollection(theArray);
public function addEntry(_name:String, _type:String):void
{
this.allCollection.addItem({label:_name, gender:_type});
this.femaleCollection.refresh();
this.maleCollection.refresh();
}
public function filterMale(item:Object):Boolean
{
return item.gender == "Male";
}
public function filterFemale(item:Object):Boolean
{
return item.gender == "Female";
}
public function onCreationComplete():void
{
maleCollection.filterFunction = filterMale;
femaleCollection.filterFunction = filterFemale;
addEntry("Male 1", "Male");
addEntry("Female 1", "Female");
addEntry("Male 2", "Male");
addEntry("Female 2", "Female");
}
]]>
</mx:Script>
<mx:Label text="Male"/>
<mx:List dataProvider="{maleCollection}"
width="300"/>
<mx:HRule width="200"/>
<mx:Label text="Female"/>
<mx:List dataProvider="{femaleCollection}"
width="300"/>
<mx:HRule width="200"/>
<mx:Label text="All"/>
<mx:List dataProvider="{allCollection}"
width="300"/>
<mx:HRule width="200"/>
<mx:Label text="Add new entry"/>
<mx:HBox>
<mx:TextInput id="entryName"
text="Name here"/>
<mx:ComboBox id="type">
<mx:dataProvider>
<mx:Object label="Male"/>
<mx:Object label="Female"/>
</mx:dataProvider>
</mx:ComboBox>
<mx:Button label="Add"
click="addEntry(entryName.text, type.selectedLabel)"/>
</mx:HBox>
</mx:Application>
1. | <?xml version="1.0" encoding="utf-8"?> |
2. | <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" |
3. | layout="vertical" |
4. | creationComplete="onCreationComplete()"> |
5. | <mx:Script> |
6. | <![CDATA[ |
7. | import mx.collections.ArrayCollection; |
8. | public var theArray:Array = new Array(); |
9. | [Bindable] |
10. | public var maleCollection:ArrayCollection = new ArrayCollection(theArray); |
11. | [Bindable] |
12. | public var femaleCollection:ArrayCollection = new ArrayCollection(theArray); |
13. | [Bindable] |
14. | public var allCollection:ArrayCollection = new ArrayCollection(theArray); |
15. | |
16. | public function addEntry(_name:String, _type:String):void |
17. | { |
18. | this.allCollection.addItem({label:_name, gender:_type}); |
19. | this.femaleCollection.refresh(); |
20. | this.maleCollection.refresh(); |
21. | } |
22. | |
23. | public function filterMale(item:Object):Boolean |
24. | { |
25. | return item.gender == "Male"; |
26. | } |
27. | |
28. | public function filterFemale(item:Object):Boolean |
29. | { |
30. | return item.gender == "Female"; |
31. | } |
32. | |
33. | public function onCreationComplete():void |
34. | { |
35. | maleCollection.filterFunction = filterMale; |
36. | femaleCollection.filterFunction = filterFemale; |
37. | addEntry("Male 1", "Male"); |
38. | addEntry("Female 1", "Female"); |
39. | addEntry("Male 2", "Male"); |
40. | addEntry("Female 2", "Female"); |
41. | } |
42. | ]]> |
43. | </mx:Script> |
44. | |
45. | <mx:Label text="Male"/> |
46. | <mx:List dataProvider="{maleCollection}" |
47. | width="300"/> |
48. | <mx:HRule width="200"/> |
49. | |
50. | <mx:Label text="Female"/> |
51. | <mx:List dataProvider="{femaleCollection}" |
52. | width="300"/> |
53. | <mx:HRule width="200"/> |
54. | |
55. | <mx:Label text="All"/> |
56. | <mx:List dataProvider="{allCollection}" |
57. | width="300"/> |
58. | <mx:HRule width="200"/> |
59. | |
60. | <mx:Label text="Add new entry"/> |
61. | <mx:HBox> |
62. | <mx:TextInput id="entryName" |
63. | text="Name here"/> |
64. | <mx:ComboBox id="type"> |
65. | <mx:dataProvider> |
66. | <mx:Object label="Male"/> |
67. | <mx:Object label="Female"/> |
68. | </mx:dataProvider> |
69. | </mx:ComboBox> |
70. | <mx:Button label="Add" |
71. | click="addEntry(entryName.text, type.selectedLabel)"/> |
72. | </mx:HBox> |
73. | </mx:Application> |
74. |
Subscribe to:
Post Comments (Atom)
2 comments:
This was very helpful. This is exactly what I was trying to do, but I didn't realize (but I suspected) that I needed to refresh the filtered collections. I didn't think you would need to refresh if you used the ArrayCollection methods and not the underlying Array methods to add and remove items.
@gorman
I'm glad this helped. Well each ArrayCollection is independent from the other ACs that work on the array, so it can't be notified when the other adds/removes from the array.
Post a Comment