如何在 ExtJs 文本字段中显示大写值而不显示用户字母从小写到大写的转换?

发布于 2024-12-02 09:12:48 字数 472 浏览 2 评论 0原文

我们正在使用 ExtJS 4 创建一个应用程序,该应用程序要求表单文本字段中的条目应始终为大写。

为此,我发现我可以在更改事件上调用函数,并将当前值转换为大写,然后按以下方式将其设置回字段:

change: function(field, newValue){
   field.setValue(newValue.toUpperCase());
} 

这样做的作用是,如果用户输入小写字母,则会将其转换改为大写并将其放回字段中。在此期间,会向用户显示从小写到大写的轻微过渡。也就是说,用户能够看到小写字母,并且可能在一毫秒之后,该字母变成大写字母。

问题是:有没有办法避免这种从小写到大写的“转换/转换”,并在用户输入内容后立即向用户显示大写字母?

我尝试使用 - style=文本转换:大写 - 但没有运气。

任何帮助将不胜感激。

提前致谢。

We are creating an application using ExtJS 4 which has a requirement that entry in a form textfield should always be in UPPERCASE.

For this, I found that I can call a function on change event and convert the current value to uppercase and set it back to the field in following way:

change: function(field, newValue){
   field.setValue(newValue.toUpperCase());
} 

What this does is that if a user enters a letter in lowercase, then it converts it to uppercase and puts it back in the field. During this, there is a slight transition displayed to the user from lower to upper case. That is, the user is able to see the letter in lower case and after a millisecond may be, the letter becomes uppercase.

The question is: Is there a way to avoid this 'transition/transformation' from lower to upper case and show letters in uppercase directly to the user as soon as he types something?

I tried using - style=text-transform:uppercase - but no luck.

Any help would be really appreciated.

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

淤浪 2024-12-09 09:12:48

我尝试使用 - style=text-transform:uppercase - 但没有运气。

您应该使用 fieldStyle< /code>相反。 这里是演示
干杯!

I tried using - style=text-transform:uppercase - but no luck.

You should have used fieldStyle instead. Here is demo.
Cheers!

不知所踪 2024-12-09 09:12:48

给定的答案仅适用于以大写形式显示文本,但值保持不变。我扩展了文本字段以覆盖 getValue() 方法,以返回大写的值,不仅用于显示,还带有可选的配置标志:

Ext.define('Dev.ux.UpperTextField', {
    extend: 'Ext.form.field.Text',
    alias: 'widget.uppertextfield',

    //configuration
    config: {
        uppercaseValue: true //defaults to true
    },

    constructor: function (config) {
        this.initConfig(config);
        this.callParent([config]);
    },

    initComponent: function () {
        var me = this;
        Ext.apply(me, {
            fieldStyle: 'text-transform:uppercase',
        });

        me.callParent();
    },

    //overriden function
    getValue: function () {
        var val = this.callParent();
        return this.getUppercaseValue() ? val.toUpperCase() : val;
    }
});

然后我可以轻松地将多个文本字段添加到表单中:

{
     xtype: 'uppertextfield',
     uppercaseValue: false, //custom cfg available (default is true)
     name: 'Descricao',
     fieldLabel: 'Nome da Cidade',
     allowBlank: false,
     enforceMaxLength: true,
     maxLength: 80
},

适用于 EXT 4.2.1。

The given answer works only for DISPLAYING the text in uppercase, but the value remains the same. I've extended the textfield to override the getValue() method to return the value in uppercase, not only for displaying, with an optional config flag:

Ext.define('Dev.ux.UpperTextField', {
    extend: 'Ext.form.field.Text',
    alias: 'widget.uppertextfield',

    //configuration
    config: {
        uppercaseValue: true //defaults to true
    },

    constructor: function (config) {
        this.initConfig(config);
        this.callParent([config]);
    },

    initComponent: function () {
        var me = this;
        Ext.apply(me, {
            fieldStyle: 'text-transform:uppercase',
        });

        me.callParent();
    },

    //overriden function
    getValue: function () {
        var val = this.callParent();
        return this.getUppercaseValue() ? val.toUpperCase() : val;
    }
});

Then I can easily add several textfields to a form:

{
     xtype: 'uppertextfield',
     uppercaseValue: false, //custom cfg available (default is true)
     name: 'Descricao',
     fieldLabel: 'Nome da Cidade',
     allowBlank: false,
     enforceMaxLength: true,
     maxLength: 80
},

Works in EXT 4.2.1.

小伙你站住 2024-12-09 09:12:48

您也可以使用插件来完成。

{
    xtype: 'textfield',
    plugins: ['uppertextfield']
}

在 ExtJS 4.2.1 中使用此插件

Ext.define('App.plugin.UpperTextField', {
    extend: 'Ext.AbstractPlugin',
    alias: 'plugin.uppertextfield',

    init: function (cmp) {
        Ext.apply(cmp, {
            fieldStyle: (cmp.fieldStyle ? cmp.fieldStyle + ';' : '') + 'text-transform:uppercase',
            getValue: function() {
                var val = cmp.__proto__.getValue.apply(cmp, arguments);
                return val && val.toUpperCase ? val.toUpperCase() : val;
            }
        });
    }
});

灵感来自:https://www.sencha.com/forum/showthread.php?94599-Uppercase-TextField-plugin&p=522068&viewfull=1#post522068

PS:我必须在原型上调用 getValue。如果我在 cmp 上调用 getValue,它将递归地继续执行此操作并且永远不会退出。我愿意接受有关如何通过插件更改组件的 getValue 方法的建议。

You can do it with a plugin too.

{
    xtype: 'textfield',
    plugins: ['uppertextfield']
}

Using this as the plugin in ExtJS 4.2.1

Ext.define('App.plugin.UpperTextField', {
    extend: 'Ext.AbstractPlugin',
    alias: 'plugin.uppertextfield',

    init: function (cmp) {
        Ext.apply(cmp, {
            fieldStyle: (cmp.fieldStyle ? cmp.fieldStyle + ';' : '') + 'text-transform:uppercase',
            getValue: function() {
                var val = cmp.__proto__.getValue.apply(cmp, arguments);
                return val && val.toUpperCase ? val.toUpperCase() : val;
            }
        });
    }
});

Inspired by: https://www.sencha.com/forum/showthread.php?94599-Uppercase-TextField-plugin&p=522068&viewfull=1#post522068

PS: I had to call getValue on the prototype. If I would have called getValue on the cmp it would recursively continue to do that and never exit. I'm open to suggestions on how to change the component's getValue method through the plugin.

对你的占有欲 2024-12-09 09:12:48

它适用于 ExtJS 4.2

如果您只想为文本字段输入大写字母,则扩展基本文本字段以将字段样式更改为大写,并附加一个侦听器以将任何文本更改为大写时更新原始值。

//定义一个新的自定义文本字段

Ext.define('IN.view.item.UpperCaseTextField', {
    extend: 'Ext.form.field.Text',
    alias : 'widget.myUpperCaseTextField',
    fieldStyle: {
        textTransform: "uppercase"
    },
    initComponent: function() {
        this.callParent(arguments);
    },
    listeners: {
        change: function (obj, newValue) {
            console.log(newValue);
            obj.setRawValue(newValue.toUpperCase());
        }
     }
});  

//使用别名在视图定义中使用新创建的自定义文本字段

xtype: 'form',
 items: [
  {
   xtype: 'myUpperCaseTextField',
   itemId: 'itemNumber',
   name : 'item',
   allowBlank: false,
   msgTarget: 'side',
   fieldLabel: 'Item Number',
   size: 11,
   maxLength: 10
  },


 ]

It Works for ExtJS 4.2

If you want only uppercase input for your text field then extend the base text field to change the field style to uppercase and also attach a listener to update the raw value on any text change to uppercase.

//define a new custom text field

Ext.define('IN.view.item.UpperCaseTextField', {
    extend: 'Ext.form.field.Text',
    alias : 'widget.myUpperCaseTextField',
    fieldStyle: {
        textTransform: "uppercase"
    },
    initComponent: function() {
        this.callParent(arguments);
    },
    listeners: {
        change: function (obj, newValue) {
            console.log(newValue);
            obj.setRawValue(newValue.toUpperCase());
        }
     }
});  

//use the newly created custom text field in your view definition using the alias

xtype: 'form',
 items: [
  {
   xtype: 'myUpperCaseTextField',
   itemId: 'itemNumber',
   name : 'item',
   allowBlank: false,
   msgTarget: 'side',
   fieldLabel: 'Item Number',
   size: 11,
   maxLength: 10
  },


 ]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文