Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
368 views
in Technique[技术] by (71.8m points)

javascript - ExtJS 4 and its new MVC: grid: how to handle keys?

I'm looking for a way to handle the key in the Grid. I've closely followed the examples here: http://www.sencha.com/learn/architecting-your-app-in-ext-js-4-part-1 http://www.sencha.com/learn/architecting-your-app-in-ext-js-4-part-2
http://www.sencha.com/learn/the-mvc-application-architecture/

So now everything works fine, but I'd like to handle keys in my Grid.

So I guess in the declaration "this.control({})" I should just add another event concerning userlist but it seems Grid don't have the "keypress" event.

Any idea how I should do (moreover how I should do with the new MVC pattern)?

Ext.define('GS.controller.Users', {

    extend: 'Ext.app.Controller',

    models: [
        'User'
    ],

    stores: [
        'Users'
    ],  

    views: [
        'user.List',
        'user.Edit'
    ],  

    init: function () {
        this.control({
            /* (!) Actions in 'userlist' */
            'userlist': {
                selectionchange: this.userListSelectionChange,
                itemdblclick: this.userEdit
            },  
            'userlist button[action=create]': {
                click: this.userCreate
            },  
            'userlist button[action=delete]': {
                click: this.userDelete
            },  
            /* (!) Actions in 'useredit' */
            'useredit button[action=create]': {
                click: this.userCreateValidate
            },  
            'useredit button[action=save]': {
                click: this.userEditValidate
            }   
        }); 
    },  

    userListSelectionChange: function(grid, selections, options) {
        var panel = grid.view.up('panel'),
            button = panel.down('button[action=delete]');

        button.setDisabled(selections.length === 0); 
    },  

    userCreate: function(button) {
        /* Using Ext.create() to pass variable create:true
         * instead of the shortcut:
         * var view = Ext.widget('useredit');
         */
        var view = Ext.create('GS.view.user.Edit', {
            create:true
        }); 
    },  

    userCreateValidate: function(button) {
        var win    = button.up('window'),
            form   = win.down('form'),
            values = form.getValues();

        this.getUsersStore().add(values);
        this.getUsersStore().sync();
        win.close();
    },  

    userEdit: function(grid, record) {
        var view = Ext.widget('useredit');
        view.down('form').loadRecord(record);
    },  
    userEditValidate: function (button) {
        var win    = button.up('window'),
            form   = win.down('form'),
            record = form.getRecord(),
            values = form.getValues();

        record.set(values);
        win.close();
        this.getUsersStore().sync();
    },

    userDelete: function(button) {
        var panel     = button.up('panel'),
            selection = panel.getSelectionModel().getSelection()[0];

        if (selection) {
            var store = this.getUsersStore();
            store.remove(selection);
            store.sync();
        }
    }
});
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Define KeyMap(s) in your launch: function() {...} right after you create the view.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...