1

I'm adding the virtual keyboard from http://www.greywyvern.com/code/javascript/keyboard to a text field of an extjs 4.2 form.

It basically works, see here: http://jsfiddle.net/g5VN8/1/

1) My first question is: is this really the best way to connect them? Looks ugly to me with a timer instead of events to keep the extjs value up to date.

Plus I can't overcome the following two issues:

2) the keyboard icon is wrapped to a new line. It should instead be at the end of the field, on the right side, just as in the examples here: http://www.greywyvern.com/code/javascript/keyboard

3) The field focus doesn't work. I have it in a show listener. Even when wrapped in a window.setTimeout() it doesn't work, so it's not a timing issue. No error is thrown.

Here is a copy-paste (stackoverflow's rules). I'll keep both places up to date.

Ext.onReady(function() {    
    Ext.QuickTips.init();

    var formPanel = Ext.create('Ext.form.Panel', {
        renderTo: Ext.getBody(),
        bodyStyle: 'padding: 5px 5px 0 5px;',
        defaults: {
            anchor: '100%',
         },
        items: [{
            xtype:'textfield',
            name: 'string',
            fieldLabel: 'String',
            maxLength:30, enforceMaxLength:true,
            allowBlank: false,
            listeners: {
                show: function(field) {
                    //focus the field when the window shows
                    field.focus(true, 1000); //TODO: doesn't work, no error
                },
                afterrender:function(cmp){
                    cmp.inputEl.set({ //see http://jsfiddle.net/4TSDu/19/
                        autocomplete:'on'
                    });

                    //attach the keyboard
                    //because it modifies the dom directly we need to hack it to 
                    //inform extjs (really, ext has no such listener option?)
                    var interval = window.setInterval(function() {
                        try {
                            var newValue = cmp.inputEl.dom.value;
                            var oldValue = cmp.getValue();
                            if (newValue != oldValue) {
                                //only do it then, cause it also moves the cursor 
                                //to the end and that sucks.
                                cmp.setValue( newValue );
                            }
                        } catch (e) {
                            //form was removed
                            window.clearInterval(interval);
                        }
                    }, 100);
                    // see http://www.greywyvern.com/code/javascript/keyboard
                    VKI_attach(cmp.inputEl.dom); 
                }
            }
        }],
        buttons: [{
            text: 'Alert string',
            handler: function() {
                var stringField = this.up('form').getForm().findField('string');
                alert(stringField.getValue());
            }
        }]
    });
});
4

2 回答 2

0
  1. 您可以将侦听器附加到键盘,当用户单击 VKI 键时,您会触发文本字段更改事件。

    Ext.getBody().on({
    mousedown: function (ev) {
        if (ev.target.tagName === 'TD') {
            // We trigger change event only on textfield with the focus
            if (document.activeElement) {
                if (document.activeElement.id === cmp.inputEl.dom.id) cmp.fireEvent('change');
            }
        }
    }, 
    delegate: '#keyboardInputMaster'
    });
    
  2. 这是因为 ExtJS 4 用“style=width:100%”写入输入字段。一种简单的方法是向文本字段添加负边距

    fieldStyle: 'margin-right:-40px'

  3. 奇怪的 ExtJS 行为。您必须关注输入元素,而不是文本字段组件

    Ext.defer(function () {
        cmp.inputEl.dom.focus();
    }, 100);
    

你可以在这里看到整个解决方案:http: //jsfiddle.net/EGbLn/3/

于 2013-08-22T07:19:42.960 回答
0
  1. 避免使用计时器。请改用常规的 dom 事件侦听器。

    afterrender: function (cmp) {
        ...
    
        // simply attach this to the change event from dom element
        cmp.inputEl.dom.addEventListener('change', function(){
            cmp.setValue(this.value);
        });
    
        ...
    }
    
  2. (已由 Samy Rancho 回答)

    fieldStyle: 'margin-right:-40px',
    
  3. 再次,避免使用计时器和类似的东西。只需添加以下内容:

    afterrender: function (cmp) {
        ...
    
        //focus on field
        cmp.inputEl.dom.focus();
    
        ...
    }
    

在这里找到更新的小提琴:http: //jsfiddle.net/g5VN8/11/

于 2015-03-19T19:47:52.147 回答