Now that with Ext Core a light-weight version (just 25kB) of ExtJS has been released, I thought it’s time to build a nice example with it.
The result is something very useful that you can see in actually a lot of web sites: Display a info text inside of a input text field if it is empty and does not have the focus.
Here’s how it looks like:
E-Mail:
To add this behaviour to a input field you just have to call this single line:
Extensive.behaviours.setInfoText(inputElement, 'Your email like max@mustermann.de');
As always you can find the code for this component in a library called ‘extensive‘. Feel free to support the project by participating.
Topics: · core, ext, ExtJS, field, focus, html, info, input, lost, text
I just had the requirement to mark required fields in a ExtJS form. I started with the code from Jason (Thanks!) and extended it a bit.
Now you may customize how the field marker would look like by assigning a CSS class to a requiredFieldCls property. I also added a descriptive tooltip for the marker – just take a look to the example below:
Here’s the code that produces the example:
Ext.onReady(function(){
Ext.QuickTips.init();
Ext.form.Field.prototype.msgTarget = 'under';
Extensive.components.RequiredFieldInfo.prototype.requiredFieldText = 'These are really important things';
var panel = new Ext.FormPanel({
frame: true,
labelWidth: 130,
width: 350,
items: [{
xtype: 'textfield',
fieldLabel: 'This you might tell me',
anchor: '100%'
}, {
xtype: 'textfield',
fieldLabel: 'This I have to know',
allowBlank: false,
anchor: '100%',
blankText: 'As said, I have to know this!'
}, {
xtype: 'reqFieldInfo'
}]
});
var element = Ext.query('script[src$=required-field.js]')[0];
var renderElement = element.parentNode;
panel.render(renderElement);
});
Dont’ forget to also include these necessary modifications to Ext.layout.FormLayout:
Ext.ns('Extensive.components');
Ext.apply(Ext.layout.FormLayout.prototype, {
originalRenderItem: Ext.layout.FormLayout.prototype.renderItem,
renderItem: function(c, position, target){
if (c && !c.rendered && c.isFormField && c.fieldLabel && !c.allowBlank) {
c.fieldLabel = c.fieldLabel + " <span " +
((c.requiredFieldCls !== undefined) ? 'class="' + c.requiredFieldCls + '"' : 'style="color:red;"') +
" ext:qtip=\"" +
((c.blankText !== undefined) ? c.blankText : "This field is required") +
"\">*</span>";
}
this.originalRenderItem.apply(this, arguments);
}
});
Extensive.components.RequiredFieldInfo = Ext.extend(Ext.form.Label, {
constructor: function(config){
Extensive.components.RequiredFieldInfo.superclass.constructor.call(this, Ext.apply({
html: "<span " +
((this.requiredFieldCls !== undefined) ? 'class="' + this.requiredFieldCls + '"' : 'style="color:red;"') +
'>*</span> ' +
((this.requiredFieldText !== undefined) ? this.requiredFieldText : 'Required field')
}, config));
}
});
Ext.reg('reqFieldInfo', Extensive.components.RequiredFieldInfo);
From know on you can find this code and all my other ExTJS components bundled in a library called ‘extensive’ – hosted at Google Code. Feel free to support the project by participating.
Topics: · ext, ExtJS, field, form, required
Wouldn’t it be nice to use a combo box of flag images to select the country code of phone numbers?
If you use a country selection component that’s quite easy. Just wrap it in a re-usable component together with a textfield for the local number and the result looks like this:
And here’s the example’s source:
Ext.onReady(function(){
var panel = new Ext.FormPanel({
style: 'padding: 10px;',
frame: true,
labelWidth: 50,
width: 400,
items: [{
fieldLabel: 'Mobile',
xtype: 'phonefield',
anchor: '100%',
emptyText: '(e.g.: 423-423-423)',
defaultCountryCode: '49',
maskRe: /[0-9 -]/
}]
});
var element = Ext.query('script[src$=phonefield.js]')[0];
var renderElement = element.parentNode;
panel.render(renderElement);
});
Update: You can find this code and all my other ExtJS components bundled in a library called ‘extensive’. Enjoy the code and feel free to support the project by participating.
Topics: · code, country, ext, ExtJS, field, number, phone, selector
Ext already provides a component to select multiple items. The thing I do not like about it is that it covers too much space on the screen.
Therefore I thought of a component that initially looks like a combo box and then opens a new window to let the user select the items.
As a nice example tells more than thousand words – here it is:
As you can see, it is actually based on the component provided by the Ext examples.
And here’s the source code for the example:
Ext.onReady(function(){
var model = new Ext.data.SimpleStore({
fields: ['label', 'value'],
data: [['Homer', 1], ['Marge', 2], ['Maggie', 3], ['Bart', 4], ['Lisa', 4]]
});
var panel = new Ext.FormPanel({
frame: true,
labelWidth: 50,
width: 300,
items: [{
fieldLabel: 'I like',
xtype: 'singleLineMultiSelect',
store: model,
anchor: '90%',
mode: 'local',
displayField: 'label',
fromLegend: 'available',
toLegend: 'selected',
valueField: 'value',
deleteText: 'delete',
emptyText: 'Which Simpsons do you like?'
}]
});
var element = Ext.query('script[src$=ext-multiselector.js]')[0];
var renderElement = element.parentNode;
panel.render(renderElement);
});
Altough the example binds an in-memory store, you can also use an external store. If so, please remove the mode property – similar to a combo box.
Update: You can find this code and all my other ExtJS components bundled in a library called ‘extensive’. Enjoy the code and feel free to support the project by participating.
Topics: · combo, Component, ext, items, multiple, select, selector
Actually it is pretty easy to get the values of all fields of a form in Ext: You just have to call the getValues() method of the BasicForm.
The problem with that approach is that it just takes the values directly from the DOM and is not calling the getValue() for each field (Which I assumed). One consequence is that you don’t get the values of combo boxes, but their labels.
To get the real values, I just did the following:
Firstly, I retrieved an array of all fields of the form:
var fields = that.findBy(function(comp) {
return comp.xtype != 'fieldset';
});
(In my case these were just all children that were no fieldsets. You might have to change the condition above to suit your needs.)
Then, I just iterated over all fields and added the name/value pair to a result object:
var result = {};
fields.forEach(function(field){
var name = field.getName();
var value = field.getValue();
result[name] = value;
});
That’s it – now result contains the real values of all fields.
Topics: · combo, ext, form, values