How to create custom ExtJS form field component?

后端 未结 9 579
无人共我
无人共我 2020-12-02 11:34

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 doc

9条回答
  •  醉酒成梦
    2020-12-02 12:02

    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)
        }
    });
    

提交回复
热议问题