刷新后网格选择丢失

时间:2012-08-07 07:14:53

标签: extjs grid refresh selection

在extjs中我有一个网格。此网格每**秒刷新一次。为了保持网格选择,我使用它:

     Ext.define('PersistantSelectionGridPanel', {
    extend: 'Ext.grid.Panel',
    selectedRecords: [],
    initComponent: function () {
        this.callParent(arguments);

        this.getStore().on('beforeload', this.rememberSelection, this);
        this.getView().on('refresh', this.refreshSelection, this);
    },
    rememberSelection: function (selModel, selectedRecords) {
        if (!this.rendered || Ext.isEmpty(this.el)) {
            return;
        }

        this.selectedRecords = this.getSelectionModel().getSelection();
        this.getView().saveScrollState();
    },
    refreshSelection: function () {
        if (0 >= this.selectedRecords.length) {
            return;
        }

        var newRecordsToSelect = [];
        for (var i = 0; i < this.selectedRecords.length; i++) {
            record = this.getStore().getById(this.selectedRecords[i].getId());
            if (!Ext.isEmpty(record)) {
                newRecordsToSelect.push(record);
            }
        }

        this.getSelectionModel().select(newRecordsToSelect);
        Ext.defer(this.setScrollTop, 30, this, [this.getView().scrollState.top]);
    }
});

但这在一个网格中不起作用,我无法找出原因。这是刷新功能:

refreshSeconds = refreshRate * 1000;
refreshData = {
    run: function () {
        NL.store.load();
    },
    interval: refreshSeconds
}
Ext.TaskManager.start(refreshData)

这是网格(没有列)

 var grid = Ext.create('PersistantSelectionGridPanel', {
   autoscroll: true,
    region: 'center',
    store: NL.store,
    multiSelect: false,
    stateful: true,
    loadMask: false,
    stateId: 'stateGridEvents',
    viewConfig: {
        stripeRows: true
    },
    columns: [{

但是当重新加载商店时,选择会丢失。我需要改变什么?

2 个答案:

答案 0 :(得分:1)

要获取所选记录,请使用

grid.getSelectionModel().getSelections();

在你的代码中,getSelections()中缺少's'。你可能会遇到一个脚本错误。

我认为下面的代码应该修改为

Your code::     this.getView().on('refresh', this.refreshSelection, this);
Modified code:  this.getStore().on('afterload', this.refreshSelection, this);

以便在加载数据后选择记录。

答案 1 :(得分:-1)

我会首先尝试使用标准功能:

Ext.define('PersistentSelectionGridPanel', {
    extend: 'Ext.grid.Panel',

    viewConfig: {
        preserveScrollOnRefresh: true
    },

    ...
});