简体   繁体   English

组合框更改的动态表单字段在extjs 4中

[英]Dynamic form fields on change of combobox In extjs 4

I have a combobox and now I want to create a dynamic textfields on change of this combo box in Extjs 4 and i am following the Mvc structure of Extjs . 我有一个组合框,现在我想在Extjs 4中改变这个组合框创建动态文本字段,我遵循Extjs的Mvc结构。 Mycombo is below Mycombo在下面

        {
                                    xtype : 'combo',
                                    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(combo, value) {}
}

You can add a FieldSet like this to your form 您可以将这样的FieldSet添加到表单中

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

so when the combobox 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 combobox value. 这样,您可以根据组合框值确定fieldsToAdd包含的内容。

Set an id to the textfield, then configure the listeners property of your combo as follows : 将id设置为textfield,然后按如下方式配置组合的listeners属性:

listeners: {
    change: function (combo, 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: 'combo',
        // 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()
        });
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM