记住刷新后在extjs网格中选择的行

remember after refresh selected row in extjs grid

我有问题。我使用extjs grid。该网格将每隔seconds刷新一次。

我使用此功能刷新:

1
2
3
4
5
6
7
ND.refresh = function() {
    ND.commList.load();
}


var refreshSeconds = refreshRate * 1000;
var t = setInterval('ND.refresh()', refreshSeconds);

但是当某人选择某行以突出显示该行时reset此选择。
如何记住所选行并在刷新后再次突出显示它?

这是我的网格:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
var grid = Ext.create('Ext.grid.Panel', {
     autoscroll: true,
     region: 'center',
     store: ND.dashBoardDataStore,
     stateful: true,
     forceFit: true,
     loadMask: false,
     stateId: 'stateGrid',

     viewConfig: {
         stripeRows: true
     },
     columns: [{
         text: 'Vehicle',
         sortable: true,
         flexible: 1,
         width: 60,
         dataIndex: 'vehicle'
     }, {
         text: 'CCU',
         sortable: true,
         flexible: 0,
         width: 50,
         renderer: status,
         dataIndex: 'ccuStatus'
     }]
 });

谢谢大家


我编写了一个简单的Ext.grid.Panel扩展名,该扩展名自动选择了在重新加载商店之前选择的返回行。您可以在此jsFiddle

中尝试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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]);
    }
});


直接的解决方案是将其保存在所选行的js索引中的某个位置。然后,重新加载后,您可以使用网格的选择模型轻松地按索引选择该行。

获取选择模型:http://docs.sencha.com/ext-js/4-0/#!/api/Ext.grid.Panel-method-getSelectionModel

1
var selectionModel = grid.getSelectionModel()

获取选定的行:http://docs.sencha.com/ext-js/4-0/#!/api/Ext.selection.Model-method-getSelection

1
var selection = selectionModel.getSelection()

将选择的行设置回:http://docs.sencha.com/ext-js/4-0/#!/api/Ext.selection.Model-method-select

1
selectionModel.select(selection)


这是选择先前选择的记录的另一种方法:

1
2
3
4
5
6
7
8
9
10
var selectionModel = grid.getSelectionModel()

// get the selected record
var selectedRecord = selectionModel.getSelection()[0]

// get the index of the selected record
var selectedIdx = grid.store.indexOfId(selectedRecord.data.id);

// select by index
grid.getSelectionModel().select(selectedIdx);

由于某种原因,grid.getSelectionModel()。select(record)方法不适用于我,但按索引选择似乎有效。

编辑:找出为什么grid.getSelectionModel()。select(record)方法不起作用。显然,商店已重新加载,记录实例是不同的(它们具有不同的自动生成的Ext ID)。在这种情况下,您必须使用selectAt()。


对于extjs 4.1.7用户

需要围绕

中的声明的解决方法

refreshSelection() {

...
Ext.defer(this.setScrollTop, 30, this,
[this.getView().scrollState.top])

} ??

因此setScrollTop不再存在

因此添加了有效的解决方案
me.getView()。preserveScrollOnRefresh = true;

在initComponent

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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);

        //-------------------------------------------
        me.getView().preserveScrollOnRefresh = true;
        //-------------------------------------------
    },
    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);
    }
});


我对此代码进行了修改。
如果进行选择,并在商店上启用过滤器并重新加载,则选择模型在此所选集合中(索引0处)具有第一个空数组。

此修改位于" refreshSelection "函数的最后一行。

1
2
3
4
if (newRecordsToSelect.length >= 1){
        this.getSelectionModel().select(newRecordsToSelect);
        Ext.defer(this.setScrollTop, 30, this, [this.getView().scrollState.top]);
    }