在单个GridPanel中使用PagingToolbar和CheckboxSelectionModel

时间:2012-01-23 18:09:56

标签: 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);
});

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

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

提前致谢, 汤姆

3 个答案:

答案 0 :(得分: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;
    }

需要额外绑定到商店:

store.on('load', grid.getSelectionModel().restoreSelection, grid.getSelectionModel());

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

答案 1 :(得分:0)

Lolo的解决方案很棒,但似乎它不再适用于ExtJS 4.2.1。

而不是'selectionchange'使用:

deselect: function( selectionModel, record, index, eOpts ) {
    delete this.multipageSelection[i.id];
},

select: function( selectionModel, record, index, eOpts ) {
     this.multipageSelection[i.id] = true;
},

答案 2 :(得分:0)

这是ExtJs5的解决方案,利用MVVC创建一个名为' selectedObjects'的本地商店。在视图模型中,与分页网格具有相同的模型。

在checkboxmodel上添加select并取消选择侦听器。在这些函数中,添加或删除此本地存储中的选定或取消选择的记录。

onCheckboxModelSelect: function(rowmodel, record, index, eOpts) {
    // Add selected record to the view model store
    this.getViewModel().getStore('selectedObjects').add(record);
},

onCheckboxModelDeselect: function(rowmodel, record, index, eOpts) {
    // Remove selected record from the view model store
    this.getViewModel().getStore('selectedObjects').remove(record);
},

在分页工具栏上,添加更改侦听器,以便在页面中显示时重新选择以前选中的记录。

onPagingtoolbarChange: function(pagingtoolbar, pageData, eOpts) {
    // Select any records on the page that have been previously selected
    var checkboxSelectionModel = this.lookupReference('grid').getSelectionModel(),
        selectedObjects = this.getViewModel().getStore('selectedObjects').getRange();

    // true, true params. keepselections if any and suppresses select event. Don't want infinite loop listeners.
    checkboxSelectionModel.select(selectedObjects, true, true);
},

完成任何操作后,不再需要这些选择。如果它不是被破坏的视图,则在checkboxmodel上调用deselectAll并从本地存储中调用removeAll。 (Windows关闭,默认设置为调用destroy,如果是这种情况,将负责本地商店数据清理)