Solved

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

Posted on 2010-09-24
8
1,146 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
ID: 33753191
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
ID: 33753264
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
ID: 33753880
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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 

Author Comment

by:DReade83
ID: 33753892
Are there no real-time Flex/PHP solutions available that would make this process easier to implement?
0
 
LVL 20

Expert Comment

by:ChristoferDutz
ID: 33754001
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
ID: 33754052
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
ID: 34172055
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
ID: 34203489
Found a solution. See my post.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Consuming Web Service output 2 398
AdvancedDataGRid 4 277
adobe air 1 301
Issue running Flash Builder application in Google Chrome 2 932
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…
This article explains the steps required to use the default Photos screensaver to display branding/corporate images
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

809 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