如何在不向上滚动的情况下绑定DataGrid组件的数据?

发布于 2024-08-12 11:18:50 字数 98 浏览 7 评论 0原文

我有一个 DataGrid 组件,我想每 5 秒更新一次。当行被添加到这个 DataGrid 时,我注意到每次更新都会导致它将滚动条位置重置到顶部。如何才能将滚动条保持在之前的位置?

I have a DataGrid component that I would like to update every 5 seconds. As rows are being added to this DataGrid I noticed that every update causes it to reset the scroll bar position to the top. How can I manage to keep the scroll bar at its previous position?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

时光是把杀猪刀 2024-08-19 11:18:50

创建一个变量来存储最后的滚动位置并使用它。

大致如下:

var lastScroll:Number = 0;

private function creationCompleteHandler(event:FlexEvent):void{
stage.addEventListener(MouseEvent.MOUSE_UP, updateLastScroll);
}

private function updateLastScroll(event:MouseEvent):void{
lastScroll = myDataGrid.verticalScrollPosition
}
private function dataGridHandler(event:Event):void{
myDataGrid.verticalScrollPosition = lastScroll;
}

这不是最好的代码,但它说明了这一点,每当有人完成滚动事件时,您都会将最后一个位置存储在变量中,并在添加新数据后立即使用它来恢复滚动位置。

make a variable to store your last scroll position and use that.

roughly something like:

var lastScroll:Number = 0;

private function creationCompleteHandler(event:FlexEvent):void{
stage.addEventListener(MouseEvent.MOUSE_UP, updateLastScroll);
}

private function updateLastScroll(event:MouseEvent):void{
lastScroll = myDataGrid.verticalScrollPosition
}
private function dataGridHandler(event:Event):void{
myDataGrid.verticalScrollPosition = lastScroll;
}

It's not the best code, but it illustrates the point, whenever someone finishes the scroll event, you store last position in a variable and you use that to restore the scroll position right after you've added new data.

在你怀里撒娇 2024-08-19 11:18:50

我基于 这篇文章。看起来效果很好。

public final class DataGridEx extends DataGrid
{
    public var maintainScrollAfterDataBind:Boolean = true;

    public function DataGridEx()
    {
        super();
    }       

    override public function set dataProvider(value:Object):void {
        var lastVerticalScrollPosition:int = this.verticalScrollPosition;
        var lastHorizontalScrollPosition:int = this.horizontalScrollPosition;

        super.dataProvider = value;

        if(maintainScrollAfterDataBind) {
            this.verticalScrollPosition = lastVerticalScrollPosition;
            this.horizontalScrollPosition = lastHorizontalScrollPosition;
        }
}   

I wrote a little extension class to DataGrid based on this article. It seems to work great.

public final class DataGridEx extends DataGrid
{
    public var maintainScrollAfterDataBind:Boolean = true;

    public function DataGridEx()
    {
        super();
    }       

    override public function set dataProvider(value:Object):void {
        var lastVerticalScrollPosition:int = this.verticalScrollPosition;
        var lastHorizontalScrollPosition:int = this.horizontalScrollPosition;

        super.dataProvider = value;

        if(maintainScrollAfterDataBind) {
            this.verticalScrollPosition = lastVerticalScrollPosition;
            this.horizontalScrollPosition = lastHorizontalScrollPosition;
        }
}   
浊酒尽余欢 2024-08-19 11:18:50

我还没有测试过这段代码,但它应该可以工作:

var r:IListItemRenderer;
var len:Number = dataGrid.dataProvider.length;
var i:Number;
//indexToItemRenderer returns null for items that are not visible
for(i = 0; i < len; i++)
{
  r = dataGrid.indexToItemRenderer(i);
  if(r)
    break;
}
//now i contains the first visible item - store this in a variable
lastPos = i;

//update the dataprovider here.

//now scroll to the position
dataGrid.scrollToIndex(lastPos);

I haven't tested this code, but it should work:

var r:IListItemRenderer;
var len:Number = dataGrid.dataProvider.length;
var i:Number;
//indexToItemRenderer returns null for items that are not visible
for(i = 0; i < len; i++)
{
  r = dataGrid.indexToItemRenderer(i);
  if(r)
    break;
}
//now i contains the first visible item - store this in a variable
lastPos = i;

//update the dataprovider here.

//now scroll to the position
dataGrid.scrollToIndex(lastPos);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文