Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create custom ExtJS form field component?

I want to create custom ExtJS form field components using other ExtJS components in it (e.g. TreePanel). How can I do it most easily?

I've read docs of Ext.form.field.Base but I don't want to define field body by fieldSubTpl. I just want to write code which creates ExtJS components and maybe some other code which gets and sets values.

Update: Summarized purposes are the followings:

  • This new component should fit in the form GUI as a field. It should have label and the same alignment (label, anchor) of other fields without need of further hacking.

  • Possibly, I have to write some getValue, setValue logic. I'd rather embed it into this component than making separated code which copies things into further hidden form fields that I also have to manage.

like image 979
pcjuzer Avatar asked May 27 '11 14:05

pcjuzer


People also ask

How to create a form using ExtJS?

Before creating a form, we should know about xTypes. xType defines the type of Ext JS UI component, which is determined during rendering of the component. For example, the element can be a textbox for which we have xType as textField or the element can have a numeric value only for which we have Numeric xType.

How to submit form in ExtJS?

In Ext JS, the Form panel handles the submission of form data for you using its built-in submit method. The form collects up all of the values in the form and posts the form data to your remote server. The controller logic to handle the submission is pretty straightforward: Get the form >> form. submit() .

What is initComponent in Extjs?

initComponent This method is invoked by the constructor. It is used to initialize data, set up configurations, and attach event handlers. beforeShow This method is invoked before the Component is shown. onShow Allows addition of behavior to the show operation.

What is autoEl in Extjs?

autoEL. The autoEl config option lets you customize the Element that will encapsulate the Component. By default, all Components are wrapped in a div tag, but autoEl provides a way to specify some other tag. In addition, you can provide HTML that will be rendered inside the Element.


1 Answers

To extend @RobAgar 's answer, following a really simple Date Time field that I wrote for ExtJS 3 and it's quickport that I made for ExtJS 4. The important thing is the use of the Ext.form.field.Field mixin. This mixin provides a common interface for the logical behavior and state of form fields, including:

Getter and setter methods for field values Events and methods for tracking value and validity changes Methods for triggering validation

This can be used for combining multiple fields and let act them as one. For a total custom fieldtype I recommend to extend Ext.form.field.Base

Here is the example that I mentioned above. It should shoe how easy this can be done even for something like a date object where we need to format the data within the getter and setter.

Ext.define('QWA.form.field.DateTime', {     extend: 'Ext.form.FieldContainer',     mixins: {         field: 'Ext.form.field.Field'     },     alias: 'widget.datetimefield',     layout: 'hbox',     width: 200,     height: 22,     combineErrors: true,     msgTarget: 'side',     submitFormat: 'c',      dateCfg: null,     timeCfg: null,      initComponent: function () {         var me = this;         if (!me.dateCfg) me.dateCfg = {};         if (!me.timeCfg) me.timeCfg = {};         me.buildField();         me.callParent();         me.dateField = me.down('datefield')         me.timeField = me.down('timefield')          me.initField();     },      //@private     buildField: function () {         var me = this;         me.items = [         Ext.apply({             xtype: 'datefield',             submitValue: false,             format: 'd.m.Y',             width: 100,             flex: 2         }, me.dateCfg),         Ext.apply({             xtype: 'timefield',             submitValue: false,             format: 'H:i',             width: 80,             flex: 1         }, me.timeCfg)]     },      getValue: function () {         var me = this,             value,             date = me.dateField.getSubmitValue(),             dateFormat = me.dateField.format,             time = me.timeField.getSubmitValue(),             timeFormat = me.timeField.format;         if (date) {             if (time) {                 value = Ext.Date.parse(date + ' ' + time, me.getFormat());             } else {                 value = me.dateField.getValue();             }         }         return value;     },      setValue: function (value) {         var me = this;         me.dateField.setValue(value);         me.timeField.setValue(value);     },      getSubmitData: function () {         var me = this,             data = null;         if (!me.disabled && me.submitValue && !me.isFileUpload()) {             data = {},             value = me.getValue(),             data[me.getName()] = '' + value ? Ext.Date.format(value, me.submitFormat) : null;         }         return data;     },      getFormat: function () {         var me = this;         return (me.dateField.submitFormat || me.dateField.format) + " " + (me.timeField.submitFormat || me.timeField.format)     } }); 
like image 157
sra Avatar answered Oct 04 '22 12:10

sra