Yes, I just ran into this myself.

 

After you update the dataProvider, you must give Flex/FP time to get the
control ready for visual interactions like setting the selectedIndex
highlight.

 

Use callLater() to invoke a function that sets the selectedIndex.  Pass
the index in in the arguments array.  Post if you need more detail.

 

Tracy

 

________________________________

From: [email protected] [mailto:[EMAIL PROTECTED] On
Behalf Of greenfishinwater
Sent: Wednesday, March 28, 2007 5:08 PM
To: [email protected]
Subject: [flexcoders] Setting selectedIndex in Grid not working

 

I have a grid in which I want to move rows down when a specific
button 'Move Down' is clicked. I have this logic working and the row
selected does move down for every click on the button.

The problem I am having is that I want the row that has been selected
and that is moving down to be always highlighted in the grid as the
selected row.

My code does not work. Well on the first click to move down the
row is moved, but the wrong row is highlighted. On the next click to
move down the row is moved, and the correct row is highlighted.
This continues so that on alternate clicks the wrong row is highlighted.

Can anybody explain this or provide some code that does highlight the
correct row all the time.

I have built a simple example to illustrate the problem. When running
select the top row Orange, then click the move down button.

Andrew

** Start of Code:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml
<http://www.adobe.com/2006/mxml> "
layout="absolute" creationComplete="initApp()">
<mx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
import mx.events.ListEvent;
[Bindable] private var names:ArrayCollection;
[Bindable] private var selIdx:int = 99;
private function initApp():void {
names = new ArrayCollection([{label:"Orange"}, {label:"Apple"},
{label:"Banana"}, {label:"Grape"}, {label:"Pear"}]);
}
private function moveDown():void {
if (selIdx < names.length - 1) {
var rem:Object = names.removeItemAt(selIdx);
selIdx = selIdx + 1;
names.addItemAt(rem, selIdx);
dg.selectedIndex = selIdx;
}
}
private function rowChanged(e:ListEvent):void {
selIdx = dg.selectedIndex;
}
]]>
</mx:Script>
<mx:VBox>
<mx:DataGrid id="dg" dataProvider="{names}" rowCount="7"
itemClick="rowChanged(event)">
<mx:columns>
<mx:DataGridColumn dataField="label" width="90"/>
</mx:columns>
</mx:DataGrid>
<mx:Button label="Move Down" click="moveDown()"/>
<mx:Label text="Selected Index = {selIdx}"/>
</mx:VBox>
</mx:Application>

***** End of Code

 

Reply via email to