2014-09-10 80 views
1

我想从输入文本字段“workItemTextField”中获取文本值,但无法使用下面的代码触发按键事件。rallytextfield上的听众

Ext.define('CustomApp', { 
    extend: 'Rally.app.App', 
    componentCls: 'app', 
    _onWorkItemKeyPress: function() { 
     console.log('In _onWorkItemKeyPress'); 
     console.log(this); 
    }, 
    launch: function() { 
     this.add({ 
      title: 'Time Entry', 
      width: 800, 
      padding: 10, 
      bodyPadding: 10, 
      renderTo: Ext.getBody(), 
      layout: { 
       type: 'vbox', 
       align: 'bottom' 
      }, 
      items: [ 
      { 
       xtype: 'rallytextfield', 
       itemId: 'workItemTextField', 
       fieldLabel: 'Work Item ID', 
       labelAlign: 'top', 
       listeners: { 
        scope: this, 
        keypress: this._onWorkItemKeyPress 
       } 
      }] 
     }); 
    } 
}); 

我能得到它的火与此更换听众:

listeners: { 
    keypress: { 
     element: 'el', 
     fn: this._onWorkItemKeyPress 
    } 
} 

但它不会返回我期望的那样。 “this”是textfield,但我无法调用getValue(),并且我期望传入的属性(从查看api)不是我期望的。第一个参数是事件,第二个不确定,第三个是html元素。

我正在使用apps/2.0rc3/sdk.js。我已经查看了我可以在网上找到的所有代码,看起来我正确地做了这件事,但肯定有一些我错过了。我做错了什么?

回答

1

我可以通过使用specialkey设置为等待ENTER,然后调用_onWorkItemKeyPress(field)来获得rallytextfield的值。 field.getValue()适用于这种情况。除非您的目标不是获取用户输入的完整值,否则最好不要使用按键并等到用户表示完成输入。

Ext.define('CustomApp', { 
    extend: 'Rally.app.App', 
    componentCls: 'app', 
    items:{ html:'<a href="https://help.rallydev.com/apps/2.0rc3/doc/">App SDK 2.0rc3 Docs</a>'}, 
    launch: function() { 
     var that = this; 
     that.add({ 
      title: 'My Text', 
      itemId:'mytext', 
      width: 800, 
      padding: 10, 
      bodyPadding: 10, 
      layout: { 
       type: 'vbox', 
       align: 'bottom' 
      }, 
      items: [ 
      { 
       xtype: 'rallytextfield', 
       itemId: 'workItemTextField', 
       fieldLabel: 'Work Item ID', 
       labelAlign: 'top', 
       listeners: { 
        specialkey: function(field, e){ 
         // e.HOME, e.END, e.PAGE_UP, e.PAGE_DOWN, 
         // e.TAB, e.ESC, arrow keys: e.LEFT, e.RIGHT, e.UP, e.DOWN 
         if (e.getKey() == e.ENTER) { 
          that._onWorkItemKeyPress(field); 
         } 
         else{ 
          console.log('?'); 
         } 
        } 
      } 
      }] 
     }); 
    }, 
    _onWorkItemKeyPress:function(field){ 
     console.log('value:',field.getValue()); 
    } 
}); 
+0

谢谢!这样可行!是的,我将在keypress的函数调用中检查ENTER,但无法通过任何键调用该函数。另外,我并不知道“specialkey”。这将工作我们的伟大。仍然困惑为什么“按键”听众不工作,但这至少让我通过了我的问题。再次感谢! – 2014-09-11 12:32:23