View Full Version : Best practice: two DataGrids\one ArrayCollection
hillel369
09-24-2008, 08:22 AM
I've got an arraycollection which contains my list of users. On my page I have two datagrids which both use the same arraycollection as it's dataprovider.
I'd like to be able to filter the arraycollection but have only one of the datagrids update.
I'd guess there are 2 possible appoaches:
- Make the arraycollection not bindable
- Have one of the datagrids ignore the binding updates
If I take the first approach how do I tell the the datagrid to update it's display after the underlying arraycollectin has changed?
If I take the seconds appoach, how can I accomplish this.
Is there a 'best practice' to accomplish this sort of functionality?
Thanks,
Hillel
Peter Cowling
09-24-2008, 01:26 PM
Hi,
I would actually advocate a third approach, but onto that in a minute...
Second approach
I would not advocate the second approach. Its quite possible to do, but the most effort required to keep your data model accurate is unwarrented.
First approach
To do the first approach you would want to refresh the collection when you have finished filtering/sorting it. This refresh will trigger a collectionEvent, which you can listen out for.
I would build the listening function to act only IF ( event.kind == CollectionEventKind.REFRESH ), and then update the datagrid etc.
My preferred approach
I almost always duplicate the data collection (arraycollection in this case).
The risk is that the two collections get out of synch with your server-side data, fail to reflect user submissions, etc. This can quite easily be controlled.
The immediate benefit of this approach is far less work to control data model to display relations - although you do need to spend some of that time ensuring that appropriate data synch controls are in place. Whether you need it now or not, this approach also gives you a lot more flexibility should your requirements become more complex in the future - say you need to do things like drill-down/drop out data from one collection, and then refresh (from an untouched data model).
drkstr
09-24-2008, 04:52 PM
I second peter's approach. In situations like those, I prefer to keep the original collection untouched and use a second collection for modifications. If your collection items share the same instances of a data model, the memory impact for this is minor.
This usually works pretty well for me to keep things in sync:
[Bindable]
private var _originalCollection:ArrayCollection;
private function getModifiedCollection( data:ArrayCollection ): ArrayCollection {
var items:Array = [];
//copy items into array with any required sort/filters
return new ArrayCollection(items);
}
<mx:DataGrid dataProvider="{getModifiedCollection(_originalCollection)}" />
This way if the master list is updated, the minor list is updated with all the appropriate modifications in place.
Best Regards,
~Aaron
Peter Cowling
09-24-2008, 07:51 PM
In the interest of completeness: it is also possible to use ICollectionView. There is quite a bit more by the way of pro's and con's to this approach, but it usually tends to boil down to whether you want to (simultaneously and efficiently) work on the underlying data or not.
There is quite a bit of information out there, Adobe's site a good starter.
hillel369
09-24-2008, 08:46 PM
Thanks very much for the advice, using multiple copies of the data is working great.
Best,
Hillel
|
vBulletin® v3.8.4, Copyright ©2000-2009, Jelsoft Enterprises Ltd.