How to show/hide password in Ext.form.TextField

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-23 15:24:02

问题


could you please show me the solution to make the input text of password field show/hide when clicking another button?! I've tried to change the inputType property of that textfield, but it was rendered at that time so it didn't affect. Another way is create 2 textfields and visbile/invisible them, but I don't like do this, because it looks like cheating... Thank in advance.


回答1:


Well this post is a little old, but I thought I'd answer it anyway. Maybe it will help someone else.

You are correct that after the item is rendered it's type has been set to 'password' in the DOM. Thus we need to directly manipuate the DOM. Let's say I window that has 1 item, a FormPanel, and I have 1 item in this FormPanel, a textfield. I initially set it's inpupType: 'password' in my config options. Now I want to change that. Here is what I would do:

this.get(0).getForm().get(1).getEl().dom.type = 'text'

(I assume that this is in an event handler that has the scope of my window)

That will change the DOM and immediately show my password as text. To change it back:

this.getForm().get(1).getEl().dom.type = 'password'

In a real world situation I would not use get(index), but either set a name for the textfield and use find, or I would create a var that points to the textfield.

Hope this helps someone.

Ricky




回答2:


Yes,I too stumbled upon this. After digging in web I felt bad as there is no built in way to do this in the ext framework though it became more common requirement now days.

With the help of other people suggestions I implemented below one.

Fiddle is here https://fiddle.sencha.com/#view/editor&fiddle/25m2

Ext.tip.QuickTipManager.init();

Ext.create('Ext.form.Panel', {
    items: {
        xtype: 'fieldcontainer',

        layout: 'hbox',

        items: [{
            xtype: 'textfield',
            fieldLabel: 'Password',
            inputType: 'password',
            width: 300,
        }, {
            xtype: 'button',
            iconCls: 'fa fa-eye',
            tooltip: 'Show password',
            handler: function (button) {

                var isShowPassword = this.iconCls === 'fa fa-eye';

                this.setTooltip(isShowPassword ? 'Hide password' : 'Show password');

                this.setIconCls(isShowPassword ? 'fa fa-eye-slash' : 'fa fa-eye');

                this.prev().getEl().query('input', false)[0].set({
                    'type': isShowPassword ? 'text' : 'password'
                })
            }
        }]
    },

    renderTo: Ext.getBody()
});


来源:https://stackoverflow.com/questions/4719855/how-to-show-hide-password-in-ext-form-textfield

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!