在单个GridPanel中使用PagingToolbar和CheckboxSelectionModel

Tom*_*ell 4 extjs event-handling extjs4

我在Sencha论坛上发布了这个帖子,想在这里发布以防万一:

我有一个使用PagingToolbar和CheckboxSelectionModel的GridPanel.我想跟踪跨页面的选择.我差不多了,但是我遇到了PagingToolbar控件(例如下一页)在我的选择模型上触发'selectionchange'事件的问题.

这是我的代码的简化示例:

码:

var sm = Ext.create('Ext.selection.CheckboxModel', {
    listeners:{
        selectionchange: function(selectionModel, selectedRecords, options){
            console.log("Selection Change!!");
            // CODE HERE TO KEEP TRACK OF SELECTIONS/DESELECTIONS
        }
    }
});

var grid = Ext.create('Ext.grid.Panel', {
    autoScroll:true,
    store: store,
    defaults: {
        sortable:true
    },
    selModel: sm,
    dockedItems: [{
        xtype: 'pagingtoolbar',
        store: store,
        dock: 'bottom',
        displayInfo: true
    }],
    listeners: {'beforerender' : {fn:function(){
        store.load({params:params});

    }}}
});
store.on('load', function() {
    console.log('loading');
    console.log(params);
    console.log('selecting...');
    var records = this.getNewRecords();
    var recordsToSelect = getRecordsToSelect(records);
    sm.select(recordsToSelect, true, true);
});
Run Code Online (Sandbox Code Playgroud)

我假设我可以选择加载事件的记录而不触发任何事件.

这里发生的是在更改数据页面时触发selectionchange事件,我不希望发生这种情况.理想情况下,只有用户点击才会被跟踪为"selectionchange"事件,而不是任何其他组件的事件冒泡并在我的选择模型上触发事件.查看源代码,我可以看到在PagingToolbar上触发的唯一事件是"更改".我试图按照GridPanel,TablePanel,Gridview等处理它的方式,但我只是没有看到事件的路径.即使这样,我也不确定如何抑制从PagingToolbar到SelectionModel的事件.

先谢谢你,汤姆

Krz*_*tof 6

我设法解决了这个问题.关键是检测页面更改的位置.最简单的解决方案是为选择侦听器设置缓冲区并检查Store.loading属性.这是我选择模型的实现:

var selModel = Ext.create('Ext.selection.CheckboxModel', {
    multipageSelection: {},
    listeners:{
        selectionchange: function(selectionModel, selectedRecords, options){
            // do not change selection on page change
            if (selectedRecords.length == 0 && this.store.loading == true && this.store.currentPage != this.page) {
                return;
            }

            // remove selection on refresh
            if (this.store.loading == true) {
                this.multipageSelection = {};
                return;
            }

            // remove old selection from this page
            this.store.data.each(function(i) {
                delete this.multipageSelection[i.id];
            }, this);

            // select records
            Ext.each(selectedRecords, function(i) {
                this.multipageSelection[i.id] = true;
            }, this);
        },
        buffer: 5
    },
    restoreSelection: function() {
        this.store.data.each(function(i) {
            if (this.multipageSelection[i.id] == true) {
                this.select(i, true, true);
            }
        }, this);
        this.page = this.store.currentPage;
    }
Run Code Online (Sandbox Code Playgroud)

并且需要额外绑定到商店:

store.on('load', grid.getSelectionModel().restoreSelection, grid.getSelectionModel());
Run Code Online (Sandbox Code Playgroud)

工作样本:http://jsfiddle.net/pqVmb/