我有一个DataGrid,我将DataProvider设置为我的数据。当我的数据发生变化时,DataGrid会失去选定的行,并且滚动条会跳回顶部。如何保持选择和滚动位置?
我有一个DataGrid,我将DataProvider设置为我的数据。当我的数据发生变化时,DataGrid会失去选定的行,并且滚动条会跳回顶部。如何保持选择和滚动位置?
如果您只想保持位置不变:
在更改数据的任何函数中,首先捕获所选索引。
var myidx:int = new int(myDG.selectedIndex);
滚动位置
var myVertPos:int = new int(myDG.verticalScrollPosition);
myDG.selectedIndex = myidx;
myDG.verticalScrollPosition = myVertPos;
噢,你可能想要进行检查,确保所选索引没有超过DG中现有项的长度,如果超过了,就选择最后一个。根据我的经验,将垂直滚动位置设置为大于最大值只会导致滚动到最大值。
var myItem:Object = myDG.selectedItem;
和myDG.selectedItem = myItem;
的东西来维护项目,但是我不确定如何在没有进行大量测试的情况下解决它。然后,您需要找到滚动位置... 维护项目要复杂得多 :-) - invertedSpearoverride public function set dataProvider(value:Object):void { var vScroll:int = 0; //检查是否重新选择先前选择的项目 //并保存当前位置 if(uniqueIdField.length > 0 && selectedItem != null) { uniqueIdData = this.selectedItem[uniqueIdField]; vScroll = this.verticalScrollPosition; }
super.dataProvider = value;
if(uniqueIdField.length > 0
&& uniqueIdData != null
&& selectedItems.length <= 1)
{
当前对象:Object类型;
是否找到:Boolean类型,初始值为false;
if(dataProvider is ArrayCollection)
{
//find object in dataprovider
for(var i:int=0; i < dataProvider.length; i++)
{
currentObj = dataProvider.getItemAt(i);
if(currentObj[uniqueIdField] == uniqueIdData)
{
this.selectedItem = currentObj;
found = true;
vScroll = this.selectedIndex;
break;
}
}
if(!found)
{
this.selectedItem = null;
uniqueIdData = null;
}
}
//通知已选择、找到或为空的项目 this.verticalScrollPosition = vScroll; dispatchEvent(new ListEvent(ListEvent.CHANGE));
}