阻止CTRL-V对网格的最佳方法

时间:2017-09-04 12:50:40

标签: extjs

使用以下fiddler https://fiddle.sencha.com/#fiddle/1frn

您可以选择单元格,执行CTRL-C,然后选择不同的单元格,并执行CTRL-V,您会看到值已被复制。

如何阻止CTRL-V?

覆盖clipboard.validateAction是最好的方法吗?

privates : {
    validateAction : function(event) {
         var view =       this.getCmp().getView();
If(view.actionableMode){
return false;
}
}
}

我不清楚为什么像validateAction这样的常用函数是私有的......

1 个答案:

答案 0 :(得分:0)

您可以使用此覆盖/示例代码来控制粘贴功能,具体取决于网格的当前状态:

Ext.define('Fiddle.grid.plugin.Clipboard',{
override: 'Ext.grid.plugin.Clipboard',


beforepaste: Ext.emptyFn,

mixins: [
    'Ext.mixin.Observable'
],

constructor: function(config) {
    var me = this;

    me.callParent([config]);
    me.mixins.observable.constructor.call(me);
},

privates : {
    onPaste: function (keyCode, event) {
        var me = this,
            sharedData = me.shared.data,
            source = me.getSource(),
            i, n, s;

        if (me.validateAction(event) === false) {
            return;
        }


        if (me.fireEvent('beforepaste',keyCode,event,me.cmp) !== false) {                            

            if (source) {
                for (i = 0, n = source.length; i < n; ++i) {
                    s = source[i];


                    if (s === 'system') {
                        // get the format used by the system clipboard. 
                        s = me.getSystem();
                        me.pasteClipboardData(s);
                        break;
                    } else if (sharedData && (s in sharedData)) {
                        me.doPaste(s, sharedData[s]);
                        break;
                    }
                }
            }
        }
    }
}
});


Ext.define('UserController', {
extend : 'Ext.app.ViewController',
alias: 'controller.users',

onBeforePaste:function(keyCode,event,grid){
    //Perform custom logic
    console.log(grid)
    return false;
}
});


Ext.application({
name: 'Fiddle',


launch: function() {
    var store = Ext.create('Ext.data.Store', {
        fields: ['name', 'email', 'phone'],
        data: [{
            name: 'Lisa',
            email: 'lisa@simpsons.com',
            phone: '555-111-1224'
        }, {
            name: 'Bart',
            email: 'bart@simpsons.com',
            phone: '555-222-1234'
        }, {
            name: 'Homer',
            email: 'homer@simpsons.com',
            phone: '555-222-1244'
        }, {
            name: 'Marge',
            email: 'marge@simpsons.com',
            phone: '555-222-1254'
        }]
    });


    Ext.create('Ext.grid.Panel', {
        title: 'Simpsons',
        store: store,
        controller:'users',
        width: 400,
        renderTo: Ext.getBody(),
        columns: [{
            text: 'Name',
            dataIndex: 'name'
        }, {
            text: 'Email',
            dataIndex: 'email',
            flex: 1
        }, {
            text: 'Phone',
            dataIndex: 'phone'
        }],
        plugins: {
            ptype: 'cellediting',
            clicksToEdit: 2
        },
        selModel: {
            type: 'spreadsheet',
            rowNumbererHeaderWidth: 0
        },
        plugins: [{
            ptype: 'clipboard',
            listeners: {
                beforepaste: 'onBeforePaste'
            }
        }],
        listeners: {
            selectionchange: function(grid, selection, eOpts) {
                var store = grid.getStore();
            }
        }
    });
}
});