You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ExtJS 6.0.1 Grid定时刷新时滚动条跳转问题求助

解决ExtJS Grid刷新时滚动位置跳动的问题

针对你遇到的Grid每2秒刷新时滚动位置跳回的问题,可通过以下几种方案解决:

1. 修复重复的viewConfig配置

你的Grid代码中重复定义了viewConfig,后定义的配置会覆盖前一个,可能导致滚动保留配置未正确生效。将两个viewConfig合并为一个:

xtype: 'gridpanel',
itemId: 'devicesGrid',
width: 40,
title: 'Devices',
emptyText: 'There is no device in this group.',
store: 'Devices.GetDevices',
// 合并后的viewConfig
viewConfig: {
    itemId: 'devicesGridTableView',
    loadMask: false,
    preserveScrollOnRefresh: true,
    preserveScrollOnReload: true,
    plugins: [
        {
            ptype: 'gridviewdragdrop',
            ddGroup: 'deviceGroups',
            enableDrop: false
        }
    ]
},
// 其余配置保持不变...

2. 手动保存并恢复滚动位置

如果内置的滚动保留配置因分组功能或数据变化失效,可在刷新前后手动记录和恢复滚动位置:
修改TaskManager的run函数:

this.config.pmConfig.deviceRefreshTask = {
    run: function() {
        var store = this.getDevicesGetDevicesStore();
        var grid = this.down('#devicesGrid');
        var view = grid.getView();
        // 记录当前滚动高度
        var scrollTop = view.getEl().getScrollTop();

        store.proxy.setExtraParam('eventDisableLoader', true);
        store.load({
            callback: function() {
                // 加载完成后恢复滚动位置
                view.getEl().scrollTo('top', scrollTop);
            },
            scope: this
        });
    },
    interval: 2000,
    scope: this
};
Ext.TaskManager.start(this.config.pmConfig.deviceRefreshTask);

3. 避免在用户滚动时刷新

添加滚动状态判断,当用户正在拖动滚动条时跳过本次刷新,减少位置跳动:
首先在Grid配置中添加滚动监听:

xtype: 'gridpanel',
// 其余配置...
listeners: {
    afterrender: function(grid) {
        var view = grid.getView();
        // 标记是否正在滚动
        grid.isScrolling = false;
        view.on('scrollstart', function() {
            grid.isScrolling = true;
        });
        view.on('scrollend', function() {
            grid.isScrolling = false;
        });
    }
}

然后修改TaskManager的run函数:

this.config.pmConfig.deviceRefreshTask = {
    run: function() {
        var grid = this.down('#devicesGrid');
        // 用户正在滚动时跳过刷新
        if (grid.isScrolling) return;

        var store = this.getDevicesGetDevicesStore();
        store.proxy.setExtraParam('eventDisableLoader', true);
        store.load();
    },
    interval: 2000,
    scope: this
};

以上方案可单独或组合使用,优先尝试合并viewConfig,若仍有问题再手动控制滚动位置或避开滚动时刷新。

内容的提问来源于stack exchange,提问作者HyGy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 08:25:16