There's an example for #2 in a ComboBox on my blog.  It can apply to DG or 
List.   In 3.4 there'll be a third option (a selectedItemsCompareFunction so 
you can match UIDs

Alex Harui
Flex SDK Developer
Adobe Systems Inc.<http://www.adobe.com/>
Blog: http://blogs.adobe.com/aharui

From: [email protected] [mailto:[email protected]] On Behalf 
Of bnjmnhggns
Sent: Tuesday, April 07, 2009 9:34 PM
To: [email protected]
Subject: [flexcoders] Retaining selected item in DataGrid (or List)


I have an ArrayCollection that is the dataProvider for a DataGrid. Every 30 
seconds the objects in the ArrayCollection get replaced with new objects based 
on data received from the server. At this point, whatever selection that the 
DataGrid had now disappears, even if an object with the same UID is in the 
ArrayCollection.

What's the best way to have the DataGrid retain its selection? Obviously, an 
object that has the same UID is not enough. This makes me think that I have two 
options:

1) When updating objects in the ArrayCollection, recycle objects that have the 
same UID. This would involve copying over every property for that object.

2) When the DataGrid sees that its dataProvider has changed, first make a note 
of the selected item's UID, and then if the selected item has gone away, try to 
find an object with that UID in the dataProvider.

I know I can do option 1, though it would also entail writing code to cull 
objects that should no longer be in the ArrayCollection and add new ones. I'm 
not sure how best to do option 2.

This seems like a simple task. My question is, am I missing some better, 
simpler way to accomplish what I'm trying to do?

Thanks.

Ben

Reply via email to