Fieldset /w Dynamic Controls (9)

Leveraging ExtJS4 Store

In this example we will expand on our already-functional fieldset extension by adding an extra column to the store – “config”. This is an excellent example of the flexibility that ExtJS 4 MVC (Model View Controller) architecture allows.

DEMO & SOURCE @ JSFIDDLE

Store Changes

A trivial addition:

// Represents an input on the fieldset
Ext.define('MyCompany.model.FieldsetDynamicControls', {
    extend: 'Ext.data.Model'
    , fields: ['name','label','config']
});
 
// Possible inputs
MyCompany.globals.FieldsetDynamicControls.possibleValues = [
    { label: 'Display Name', name: 'displayName' }
    , { label: 'First Name', name: 'givenName' }
    , { label: 'Middle Name', name: 'initials' }
    , { label: 'Last Name', name: 'sn' }
    , {
        label: 'Checkbox'
        , name: 'checkbox'
        , config: { xtype: 'checkbox' }
    }
    , {
        label: 'Combo'
        , name: 'combo'
        , config: { xtype: 'combo' }
    }
    , {
        label: 'Number'
        , name: 'number'
        , config: { xtype: 'numberfield' }
    }
    , {
        label: 'Date'
        , name: 'date'
        , config: { xtype: 'datefield' }
    }
];

Controller Changes

We will now retrieve the special config from a record from our store, and apply it on top of the default “textfield” setup.

// Adds an input to the fieldset
, addControl: function(fieldset, name) {
    var controller = this
        , menu = fieldset.down('button[name=add]').menu
        , combo = menu.down('combo')
        , record = combo.findRecord('name',name)
        , field = null;
 
    // Add UI element
    var uiConfig = {
        xtype: 'textfield'
        , name: name
        , fieldLabel: record.data.label
        , msgTarget: 'under'
        , anchor: '-25'
        , allowBlank: false
        , listeners : {
            // Add "remove" button
            afterrender: function(tf) {
                var el = tf.bodyEl.createChild({tag: 'div', style: 'position: absolute; right: -23px; top: 0px;'});
                tf.innerButton = Ext.create('Ext.button.Button',{
                    renderTo: el
                    , iconCls: 'icon-remove'
                    , handler: function() {
                        // Fire custom event
                        fieldset.fireEvent('removecontrol'
                                           , fieldset
                                           , field
                                           , record);
                    }
                });
            }
            // Our button does not get destroyed when we
            // destroy the parent textfield or the parent fieldset
            , destroy: function(tf) { tf.innerButton.destroy(); }
        } // eo listeners
    };
    Ext.apply(uiConfig, record.data.config); // use custom config
    field = fieldset.add(uiConfig);
    field.focus();
 
    // Remove from "Add Direct Attr" combo
    combo.clearValue();
    combo.getStore().remove(record);
}
VN:F [1.9.22_1171]
Rating: 6.7/10 (9 votes cast)
Fieldset /w Dynamic Controls (9), 6.7 out of 10 based on 9 ratings

One thought on “Fieldset /w Dynamic Controls (9)”

Leave a Reply

Your email address will not be published. Required fields are marked *

* Copy This Password *

* Type Or Paste Password Here *