Solved

Flex 4: Can't pre-select item in DataGrid (Array)

Posted on 2010-09-24
8
1,139 Views
Last Modified: 2012-05-10
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.
0
Comment
Question by:DReade83
  • 4
  • 4
8 Comments
 
LVL 20

Assisted Solution

by:ChristoferDutz
ChristoferDutz earned 500 total points
Comment Utility
The problem is that you are using selected item ... even if the returned object has the same content ... it is not the same object (has a different object id).

What you need to do, is to save some criteria with which you can determin an equal object. Let's say the object has an id property. Then you need to save the id-value of the selected item. When getting the response you iterate over all returned objects and check their id attribute. If it matches the saved id, you set this as selected item.

If this doesn't work immediately, I would also suggest to set the item using "callLater" function because in some cases the grid needs to do some updating before you are able to set the selected item. this way you wait until Flex is finihsed updating everything and immediately after that the callLater function is executed ...
0
 

Author Comment

by:DReade83
Comment Utility
I see what you mean. Is there no way to update only the data that's changed rather than re-create the DataProvider all over again when the result is returned from the RemoteObject? Effectively the objects would remain the same, just the properties of the objects would be changing.

The reason I want to do this is because when the DataGrid updates, there's a chance there will be a pause in-between the selection disappearing and reappearing as the DataGrid is re-populated. If the DataGrid were refreshing every 5 seconds it would become noticeable and possibly annoying from the end user's perspective.

Do you know if this is possible?

Failing that, are there any real-time solutions available for a Flex/PHP environment?
0
 
LVL 20

Assisted Solution

by:ChristoferDutz
ChristoferDutz earned 500 total points
Comment Utility
Oh well ... of course ... this is a little more tricky though.

You would need to iterate over the old and the new list and to make lists of elements that were changed, added and deleted. After building these lists, you could remove the deleted and updated items and add the new and updated ones (If an updated Item was selected you would have to change the selection to the new object).

This approach is certainly the smother one, as the Table will not jump up and down while initializing.
0
 

Author Comment

by:DReade83
Comment Utility
Are there no real-time Flex/PHP solutions available that would make this process easier to implement?
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 20

Expert Comment

by:ChristoferDutz
Comment Utility
Well this is exactly what Adobes ColdFusion stuff is for ... but that is really expensive.
0
 
LVL 20

Assisted Solution

by:ChristoferDutz
ChristoferDutz earned 500 total points
Comment Utility
In the PHP world the only tool I have heard of is Zend:
http://www.flex888.com/832/zend-php-and-adobe-flex.html
http://devzone.zend.com/article/2019
Perhaps they offer components that also provide this type of functionality.
0
 

Accepted Solution

by:
DReade83 earned 0 total points
Comment Utility
I've found a solution using for(), matching one of the items in an array to the ID I want to pre-select. See attached code.
for (var i:Number = 0; i < result.length; i++)
{
    if (dataGrid.dataProvider[i].ID == _id)
    {
        dataGrid.selectedIndex = i;
        break;
    }
}

Open in new window

0
 

Author Closing Comment

by:DReade83
Comment Utility
Found a solution. See my post.
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

First things first - Preparation We need all the part for this install and it's much nicer to have them all on hand when you need them so here's what's required. Download Eclipse 3.5 32 bit (I like the Classic flavour) from here. (http://www.e…
Learn to move / copy / export exchange contacts to iPhone without using any software. Also see the issues in configuration of exchange with iPhone to migrate contacts.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now