2011-07-07 81 views
3

我正在其中一列的數據網格中執行查找操作。在找到包含該項目的行後,我將它作爲選定的索引行,將其突出顯示。但是現在我還想要向下或向上滾動數據網格(如果該項目超出了屏幕範圍),以在此查找操作中自動顯示所選項目。向下滾動數據網格或向上滾動到所選索引行flex

謝謝。

回答

4

您是否試過scrolltoindex()方法?在使用scrolltoindex()和數據網格時,請看Anuj Gakhar's article

下面是文章的例子:

<?xml version="1.0" encoding="utf-8"?> 
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" 
initialize="doInit();" creationComplete="setSelectedItem()"> 
<mx:Script> 
<![CDATA[ 
import mx.collections.ArrayCollection; 

// this holds the grid data 
[Bindable] 
private var myData:ArrayCollection = new ArrayCollection(); 
// change this name here to change the selected item on load 
[Bindable] 
private var initialFName:String = "Joe9"; 

private function doInit():void 
{ 
myData.addItem({fname:"Joe",lname:"Bloggs"}); 
myData.addItem({fname:"Joe1",lname:"Bloggs"}); 
myData.addItem({fname:"Joe2",lname:"Bloggs"}); 
myData.addItem({fname:"Joe3",lname:"Bloggs"}); 
myData.addItem({fname:"Joe4",lname:"Bloggs"}); 
myData.addItem({fname:"Joe5",lname:"Bloggs"}); 
myData.addItem({fname:"Joe6",lname:"Bloggs"}); 
myData.addItem({fname:"Joe7",lname:"Bloggs"}); 
myData.addItem({fname:"Joe8",lname:"Bloggs"}); 
myData.addItem({fname:"Joe9",lname:"Bloggs"}); 
} 

private function setSelectedItem():void 
{ 
var gData:Object = dGrid.dataProvider; 
for(var i:Number=0; i < gData.length; i++) 
{ 
var thisObj:Object = gData.getItemAt(i); 
if(thisObj.fname == initialFName) 
{ 
dGrid.selectedIndex = i; 
//sometimes scrollToIndex doesnt work if validateNow() not done 
dGrid.validateNow(); 
dGrid.scrollToIndex(i); 
} 
} 
} 
]]> 
</mx:Script> 

<mx:DataGrid id="dGrid" dataProvider="{myData}" visible="true"> 
<mx:columns> 
<mx:DataGridColumn dataField="fname" headerText="FirstName" /> 
<mx:DataGridColumn dataField="lname" headerText="LastName" /> 
</mx:columns> 
</mx:DataGrid> 

</mx:Application>