简体   繁体   English

ExtJS4即时更改TextField emptyText

[英]ExtJS4 Change TextField emptyText on the Fly

How do I change my textfield's emptyText field on the fly? 如何动态更改文本字段的emptyText字段?

I've tried: 我试过了:

myTextField.emptyText = 'new empty text';

myTextField.emptyText = 'new empty text';
myTextField.applyEmptyText();

myTextField.emptyText = ['new empty text'];

myTextField.emptyText = ['new empty text'];
myTextField.applyEmptyText();

I realize that applyEmptyText() is not listed in the API docs but I thought I'd give it a shot as this worked in previous versions. 我意识到API文档中未列出applyEmptyText() ,但我认为我可以applyEmptyText()一下,因为在以前的版本中可以使用。 I've also read that one needs to add the brackets too. 我也读过,也需要加上括号。

I've printed myTextField to the console and I see the emptyText property set to what I want but the field in the browser is not updating. 我已经myTextField打印到控制台,并且看到将emptyText属性设置为所需的属性,但是浏览器中的字段未更新。

I love ExtJS and all that it brings to the table but nuances like this (trying to get dynamically updated emptyText to work) really slow down development. 我喜欢ExtJS及其带来的所有好处,但是像这样的细微差别(试图使动态更新的emptyText起作用)确实减慢了开发速度。 None of the other SO posts on this issue have resolved the problem for me (emptyText just wouldn't update). 关于此问题的其他SO帖子都没有为我解决问题(emptyText不会更新)。

After several hours "fighting with my tools" and dozens of different permuations I came up with the following simple fix - just add a myTextField.reset(); 经过几个小时的“与我的工具搏斗”和数十种不同的配置,我想到了以下简单的解决方法-只需添加myTextField.reset();即可。 to your code block. 到您的代码块。

myTextField.emptyText = 'new empty text';
myTextField.applyEmptyText();
myTextField.reset();

Hope this saved someone else some time. 希望这可以节省一些时间。

Your first attempt should work as expected. 您的第一次尝试应能按预期工作。 However, as you can see in the source, there are certain criteria to be met before the placeholder is set. 但是,正如您在源代码中看到的那样,在设置占位符之前,需要满足某些条件。 Your best bet is to place a breakpoint in the applyEmptyText function and debug it. 最好的选择是在applyEmptyText函数中放置一个断点并对其进行调试。

applyEmptyText : function(){
    var me = this,
        emptyText = me.emptyText,
        isEmpty;

    if (me.rendered && emptyText) {
        isEmpty = me.getRawValue().length < 1 && !me.hasFocus;

        if (Ext.supports.Placeholder) {
            me.inputEl.dom.placeholder = emptyText;
        } else if (isEmpty) {
            me.setRawValue(emptyText);
            me.valueContainsPlaceholder = true;
        }

        //all browsers need this because of a styling issue with chrome + placeholders.
        //the text isnt vertically aligned when empty (and using the placeholder)
        if (isEmpty) {
            me.inputEl.addCls(me.emptyCls);
        }

        me.autoSize();
    }
}

Through debugging it seems like the Text.inputEl.dom.placeholder property was not being set during my event in time to update the field. 通过调试,好像没有在我的活动中及时设置Text.inputEl.dom.placeholder属性来更新字段。 Based on the reset source above, I found that just setting the Text.inputEl.dom.placeholder in my code worked. 根据上面的reset源,我发现仅在代码中设置Text.inputEl.dom.placeholder

This applyEmptyText method is definately buggy!! 这个applyEmptyText方法肯定有问题!

Look at the code, and imagine you want to set a zero as emptyText.... 看一下代码,想象一下要将空文本设置为零。

Look at the if conditions: 查看if条件:

if (me.rendered && emptyText)

when emptyText = 0 , in the condition it will be as a false interpretated..... it's then with this code impossible to set the zero as emptyText!!!! 当emptyText = 0时,在这种情况下它将被解释为错误的.....因此,用此代码无法将零设置为emptyText !!!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM