【问题标题】:Added an enter event to EXT JS Application search text box to fire search向 EXT JS 应用程序搜索文本框添加了一个输入事件以触发搜索
【发布时间】:2013-02-01 15:59:29
【问题描述】:

您好,下面的代码我的输入事件永远不会触发,我们将不胜感激。

    items: [{
            xtype: 'textfield',
            id: 'idhere',
            name: 'namehere',
            fieldLabel: 'lablehere:',
            width: 500,
            handler: {
                  key:13,
                  fn : function () {
                  if (e.getKey() == e.ENTER) {
                           alert("You pressed an enter button in text field.");
    }
                  }

            }
        },{
            xtype: 'button',
            text: 'texttodisplay',
            handler: function() {
                                  //my function.
            }

        }]

我实际上通过以下方式解决了这个问题:

listeners:  {
                specialkey: function (f,e) {    
                     if (e.getKey() == e.ENTER) {
                        loadData();
                    }
                }
            }

【问题讨论】:

    标签: javascript extjs keyboard-events enter


    【解决方案1】:

    我不确定为什么 Sencha 从未在 API 文档中包含 Ext.ux.form.SearchField,但该组件已包含在我使用过的所有版本的框架中。它设置为触发submit 和cancel 事件,并包括附加到字段的适当搜索和取消按钮。

    您可以在您的框架文件中找到它:[extjs-root]\examples\ux\form\SearchField.js

    我建议使用该组件,而不是尝试创建自己的搜索字段。我通常会覆盖默认搜索功能以满足我自己的需要,但在某些情况下我也不需要这样做。

    如果您在组件 JS 的顶部添加 requires 语句,您可以像任何其他(非 UX)组件一样创建它:

    例如:

    需要声明:

    Ext.define('MyApp.view.SomeComponent', {
        extend: 'Ext.grid.Panel',
        alias: 'widget.mycomponent',
        requires: [
            'Ext.ux.form.SearchField'
        ],
        ...
    

    在面板底部工具栏中创建搜索字段:

    bbar: {
        items: [{
            text: 'A Button'
        }, {
            text: 'Another Button'
        }, '-', {
            xtype: 'searchfield', // <- can use this xtype with requires stmt
            itemId: 'search',
            width: 250,
            emptyText: 'Enter first and last name to search...'
        }]
    },
    ...
    

    如果您对 requires 语句有疑问,您也可以像这样创建它:

    var search = Ext.create('Ext.ux.form.SearchField', {
        itemId: 'search',
        width: 250,
        emptyText: 'Enter first and last name to search...'
    });
    

    【讨论】:

    • 这是我第一次听说这个 ux 字段 :) +1 我猜文档丢失了,因此它没有出现在 api 中。
    【解决方案2】:

    只是提供如何添加这样的监听器。有一个specialkey 事件可以用于这种情况

    fieldinstance.on('specialkey', function(f, e){
        if (e.getKey() == e.ENTER) {
            // your action
        }
    });
    

    无论如何我建议使用 @Geronimo 提到的 ux 组件

    【讨论】:

      猜你喜欢
      • 2016-10-04
      • 2017-09-02
      • 1970-01-01
      • 2018-09-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-28
      相关资源
      最近更新 更多