【问题标题】:Focus a textfield inside a list将文本字段聚焦在列表中
【发布时间】:2019-11-18 18:09:29
【问题描述】:

ExtJS 文档为 textfields 描述了一个方法 focus,该方法将聚焦文本字段或返回 false:https://docs.sencha.com/extjs/6.7.0/modern/Ext.field.Text.html#method-focus

通常这似乎工作得很好,但是一旦应该关注的文本字段位于listlistitem 的一部分)内,焦点就会立即被移除并设置为父listitem 容器。然而,焦点返回 true(焦点实际上在场上几毫秒)。

https://fiddle.sencha.com/#view/editor&fiddle/315h 举例说明了这一点。第一个按钮尝试关注列表中的第一个文本字段(失败),第二个按钮关注list下方的字段。

有什么方法可以防止listitem 变得专注?

小提琴代码:

Ext.application({
    name: 'Fiddle',

    launch: function () {
        Ext.create({
            fullscreen: true,

            xtype: 'container',

            items: [{
                xtype: 'list',

                itemConfig: {
                    xtype: 'container',
                    items: [{
                        xtype: 'component',
                        html: 'Row'
                    }, {
                        xtype: 'textfield',
                        value: 'Focus Me'
                    }],
                    margin: '15px 0'
                },
                data: [{
                    title: 'Item 1'
                }, {
                    title: 'Item 2'
                }, {
                    title: 'Item 3'
                }, {
                    title: 'Item 4'
                }]
            }, {
                xtype: 'button',
                text: 'Try to focus',
                handler: function () {
                    this.up('container').down('list').down('textfield').focus();
                    Ext.toast(this.up('container').down('list').down('textfield').hasFocus + '');
                }
            }, {
                xtype: 'textfield',
                itemId: 'focusable',
                value: 'Focusable'
            }, {
                xtype: 'button',
                text: 'Try to focus',
                handler: function () {
                    this.up('container').down('#focusable').focus();
                    Ext.toast(this.up('container').down('#focusable').hasFocus + '');
                }
            }]
        });
    }
});

在 ExtJS 6.7 上,第一次单击列表中的文本字段甚至不起作用,但这似乎在 7.0 中得到修复。

【问题讨论】:

  • 奇怪地使用focus(true)(选择文本)可以工作,但正如你所料,它也会选择输入的文本
  • 很棒的发现!使用 focus(true) 实际上在 this 来自的用例中是可以的。

标签: javascript extjs


【解决方案1】:

listitem 容器设置defaultFocus。它为我工作 (fiddle)。

附:谢谢你的小提琴例子。很少有人这样做。

【讨论】:

  • 是的,这行得通,所以我想问题是这样的:我尝试从外部关注列表内的元素,所以焦点不在列表或列表项上。一旦焦点移动到该字段,焦点就会发生变化,并且列表正在尝试关注包含的列表项(我通过调试问题发现了这一点)。从而再次设置焦点,但这一次是列表项的defaultFocus 所暗示的任何内容。我不希望在这种情况下发生这种情况。不管怎么说,多谢拉。我会开一张票(这样就可以解决这个问题)并添加一个建议的解决方案作为解决方法。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-25
  • 2015-09-04
  • 1970-01-01
  • 2021-07-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多