最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Dynamic form fields on change of combobox In extjs 4 - Stack Overflow

programmeradmin3浏览0评论

I have a bobox and now I want to create a dynamic textfields on change of this bo box in Extjs 4 and i am following the Mvc structure of Extjs . Mybo is below

        {
                                    xtype : 'bo',
                                    store : 'product.CategoryComboBox',
                                    name: 'category',
                                    id:'category',
                                    displayField: 'name',
                                    valueField: 'idProductCategory',
                                    multiSelect : false,
                                    fieldLabel: 'Category',
                                    allowBlank: false,
                                    allowQueryAll : false,
                                    forceSelection : true,
                                    typeAhead: true,
                                    triggerAction: 'all',
                                    delimiter : ',',
                                    width: 300,
                                    queryMode:'local',
                                    listeners:{select:{fn:function(bo, value) {}
}

I have a bobox and now I want to create a dynamic textfields on change of this bo box in Extjs 4 and i am following the Mvc structure of Extjs . Mybo is below

        {
                                    xtype : 'bo',
                                    store : 'product.CategoryComboBox',
                                    name: 'category',
                                    id:'category',
                                    displayField: 'name',
                                    valueField: 'idProductCategory',
                                    multiSelect : false,
                                    fieldLabel: 'Category',
                                    allowBlank: false,
                                    allowQueryAll : false,
                                    forceSelection : true,
                                    typeAhead: true,
                                    triggerAction: 'all',
                                    delimiter : ',',
                                    width: 300,
                                    queryMode:'local',
                                    listeners:{select:{fn:function(bo, value) {}
}
Share Improve this question asked Feb 6, 2013 at 9:50 anupkumaranupkumar 3574 gold badges14 silver badges29 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 3

You can add a FieldSet like this to your form

{
    xtype: 'fieldset',
    itemId: 'field_container',
    layout: 'anchor',
    border: 0,
    style: { padding: '0' },
    fieldDefaults: {
        // field defaults
    },
    defaultType: 'textfield'
}

so when the bobox changes its value you just do the following

var container = this.down('fieldset[itemId="field_container"]');
container.removeAll();
var fieldsToAdd = [
    { name: 'field1', xtype: 'textfield', value: 'xxxxxx' },
    { name: 'field2', xtype: 'textfield', value: 'yyyyyyy' }
];
container.add(fieldsToAdd);

this way you can decide what the fieldsToAdd contains based on the bobox value.

Set an id to the textfield, then configure the listeners property of your bo as follows :

listeners: {
    change: function (bo, value) {
        Ext.get('idOfYourTextfield').setValue(value);
    }
}

Field container allows to have multiple form fields on the same line, so you could do that:

{
    xtype: 'fieldcontainer',
    layout: 'hbox',
    items: {
        xtype: 'bo',
        // your config here
        listeners: {
            change: function () {
                this.up('fieldcontainer').add({
                    xtype: 'textfield',
                    value: this.getValue()
                });
            }
        }
    }
}

Edit

I guess you'll need to test if the text field already exists:

change: function () {
    var ct = this.up('fieldcontainer'),
        textField = ct.down('textfield');
    if (textField) {
        textField.setValue(this.getValue());
    } else {
        ct.add({
            xtype: 'textfield',
            value: this.getValue()
        });
    }
}
发布评论

评论列表(0)

  1. 暂无评论