ExtJs simulate TAB on ENTER keypress

一笑奈何 提交于 2019-12-10 16:13:30

问题


I know it is not the smartest idea, but I still have to do it. Our users want to use ENTER like TAB. So, the best I came up with is this:

Ext.override(Ext.form.field.Base, {
            initComponent: function() {
                this.callParent(arguments);

                this.on('afterrender', function() {
                    var me=this;
                    this.getEl().on('keypress',function (e){
                        if(e.getKey() == 13) {
                            me.nextNode().focus();
                        }
                    });
                });
            }
        });

But it still does not work exactly the same way as TAB. I mean, it works OK with input fields, but not other controls. May be there is some low-level solution. Any ideas?


回答1:


In the past I've attached the listener to the document, something like this:

Ext.getDoc().on('keypress', function(event, target) {

    // get the form field component
    var targetEl = Ext.get(target.id),
        fieldEl = targetEl.up('[class*=x-field]') || {},
        field = Ext.getCmp(fieldEl.id);

    if (
        // the ENTER key was pressed...
        event.ENTER == event.getKey() &&

        // from a form field...
        field &&

        // which has valid data.
        field.isValid()

        ) {

        // get the next form field
        var next = field.next('[isFormField]');

        // focus the next field if it exists
        if (next) {
            event.stopEvent();
            next.focus();
        }                   
    }
});



回答2:


For Ext.form.field.Text and similar xtypes there is a extra config enableKeyEvents that needs to be set before the keypress/keydown/keyup events fire. The enableKeyEvents config option needs to be set to true as it's default to false.

ExtJS API Doc




回答3:


Disclaimer: I'm not an expert on ExtJs.

That said, maybe try something like:

if (e.getKey() === 13) {
    me.blur();
    return false;  // cancel key event to prevent the [Enter] behavior
}



回答4:


You could try this

if (e.getKey() === 13) {
    e.keyCode = Ext.EventObject.TAB
    this.fireEvent(e, {// any additional options
   });
}

Haven't really tried this ever myself.



来源:https://stackoverflow.com/questions/11682175/extjs-simulate-tab-on-enter-keypress

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!