1
votes

My Flex datagrid automatically scrolls to the top when I update the dataProvider (ArrayCollection). I do not want this to happen, but I still want all of the objects to update. I am developing a semi-real time dashboard for a customer's management system, that will update often. If it scrolls to the top every time it updates, it will be very difficult and frustrating to use.

I've attempted to use the following techniques to prevent this, none have worked.

dataProvider = updatedDataProvider;
dataProvider.updateItem(dataProvider);

dataProvider.source = updatedDataProvider.source;
dataProvider.updateItem(dataProvider);

I've attempted to dispatch a mouse event to hold the vertical scrollbar in place, I've attempted to lock the vertical scrollbar position value in place (saving and reassigning)...

And many others, those are just the first few that popped into my head. Anyone have any ideas?

2
Are you using the Spark or MX DataGrid? If you replace the dataProvider; then this is expected because Flex "Destroys" everything displayed and recreates it. However, if you just add new items onto an existing ArrayCollection, then the selected item / scroll position should be retained.JeffryHouser
@www.Flextras - MX. Flex 3 in the tags. I understand the base operation of the grid. I want it to work differently. Are you recommending I .removeAll() and then .add each item?Sam DeHaan
I didn't mean remove all. Just add your new items to the dataProvider or replace the ones that need updating. The DataGrid's collection change handler should update the grid while maintaining the scroll position (and I believe selectedItems). However, if you're updating/replacing all items in your dataProvider; then you're best bet is to store the scroll position before the replace and then set it after the replace. Although I think that would cause an odd user experience.JeffryHouser

2 Answers

2
votes

Already been answered in How do I maintain selection in a DataGrid when the data changes? , check it.

Building on top of that answer, if the index changes between data providers, ie, if a row is inserted above, you may want to do a getItemIndex on the new provider.

-1
votes

What if you select whatever was selected previously and use ensureIndexIsVisible()?