Hi
I have been, for my own amusement, trying to rewrite part of this so it will work in both extjs 4.2 and 5.0 at the same time.
Code is:
Code:
Ext.define('Ext.ux.plugins.CounterTextField', {
extend: 'Ext.util.Observable',
alias: 'plugin.counter',
constructor: function (config)
{
Ext.apply(this, config || {});
this.callParent(arguments);
},
init: function (field)
{
field.on({
scope: field,
keyup: this.updateCounter,
focus: this.updateCounter
});
if (!field.rendered) {
field.on('afterrender', this.handleAfterRender, field, {single:true});
} else {
this.handleAfterRender(field);
}
},
handleAfterRender: function(field)
{
field.counterEl = Ext.DomHelper.insertAfter(
field.inputEl,
{
tag: 'span',
style: 'width:10%;padding-left:4px',
html: field.maxLength
},
true);
field.enableKeyEvents = true;
setInterval(function(){
field.inputEl.setStyle({'display':'inline','width':'90%'},1);
});
},
updateCounter: function(textField)
{
textField.counterEl.update('' +
(textField.maxLength - textField.getValue().length));
}
});
Ext.application({
name : 'Character Counter',
launch : function() {
Ext.create('Ext.form.Panel', {
title: 'Character Counter',
bodyPadding: 5,
width: 400,
layout: 'vbox',
defaults: {
xtype: 'textfield',
width: 350,
labelWidth: 70
},
items: [{
fieldLabel: 'First Name',
maxLength: 45,
grow: false,
plugins: ['counter']
},{
fieldLabel: 'Last Name',
maxLength: 50,
plugins: ['counter']
}, {
fieldLabel: 'Address',
width: 380
}],
renderTo: Ext.getBody()
});
}
});
Would it be better to not use an Observable here and instead use a custom field container with a hbox layout?
And the layout only seems to work when I add this in:
Code:
setInterval(function(){
field.inputEl.setStyle({'display':'inline','width':'90%'},1);
});
Why is that?