tags:

views:

83

answers:

2

I'm polling a RemoteObject every 5 seconds using setInterval and the returned result (Array) is being fed into a DataGrid as the DataProvider. However everytime this happens the selected row deselects when the DataGrid is refreshed. So I want to re-select the item when the DataGrid has been updated.

So far I've tried capturing the selected row before the RemoteObject is called using:

private function refreshDataGrid(e:ResultEvent):void
{
    var selectedRow:Object = myDataGrid.selectedItem;
    myDataGrid.dataProvider = e.result as Array;
    myDataGrid.selectedItem = selectedRow;
}

However this doesn't work. If I select the row and then do a "trace(myDataGrid.selectedItem)", the result in the Console is blank.

In another attempt I tried:

private function refreshDataGrid(e:ResultEvent):void
{
    var selectedItem:* = myDataGrid.selectedItem.itemId;
    myDataGrid.dataProvider = e.result as Array;
    myDataGrid.selectedItem.itemId = selectedItem;
}

This doesn't work either.

Can anyone help me make this work? Any help would be greatly appreciated. Thanks in advance.

+1  A: 

It looks like you have a unique itemId property on your objects. The problem with your second attempt is that it is attempting to set the itemId on THE CURRENTLY SELECTED ITEM rather than changing the currently selected item to the item that has that itemId. I would change the second version to loop through the dataProvider and locate the item with the specified itemId, then set that item as the selected item. Something like this:

private function refreshDataGrid(e:ResultEvent):void
{
    var selectedItem:* = myDataGrid.selectedItem.itemId;
    myDataGrid.dataProvider = e.result as Array;

    for (var i:int = 0; i < myDataGrid.dataProvider.length; i++) {
        if (myDataGrid.dataProvider[i].itemId == selectedItem) {
            myDataGrid.selectedItem = myDataGrid.dataProvider[i];
            break;
        }
    }
}
Wade Mueller
+1  A: 

Better way of doing this is to make your objects implement the IUID interface, which is what Flex List controls use to determine whether objects match.

If the item from the latest poll has the same IUID as the old one, it will still be selected. http://livedocs.adobe.com/flex/3/html/help.html?content=about_dataproviders_8.html

Gregor Kiddie