Solved

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

Posted on 2010-09-24
8
1,147 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

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…
Originally, this post was published on Monitis Blog, you can check it here . Websites are getting bigger and more complicated by the day. Video, images and custom fonts are all great for showcasing your product or service. But the price to pay in…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

730 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