[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2010-09-24
8
Medium Priority
?
1,156 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 2000 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 2000 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
When ransomware hits your clients, what do you do?

MSPs: Endpoint security isn’t enough to prevent ransomware.
As the impact and severity of crypto ransomware attacks has grown, Webroot fought back, not just by building a next-gen endpoint solution capable of preventing ransomware attacks but also by being a thought leader.

 

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 2000 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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

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…
In the below post we have mentioned the best hosting type for startups. Also, check out some of the superlative web hosting companies that are proposing affordable web hosting solutions to host your startup website.
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses
Course of the Month17 days, 23 hours left to enroll

831 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