Extjs无法在表单面板中动态添加/删除字段

8
我有一个表单面板,使用表格布局来显示表单。我需要添加添加/删除一组组件的功能。
添加按钮应在现有元素下方添加新行组件,而删除按钮应删除最后添加的行。
Formpanel可以添加新字段,但它出现在按钮下方,表单的宽度没有增加(请参见下面的屏幕截图)。我已经尝试了插入和添加功能,但两者都具有相同的效果。
有人知道如何: 1)我可以在下一行添加一系列组件吗? 2)如何删除下一行。
部分formPanel代码和按钮代码:
![SearchForm = Ext.extend(Ext.FormPanel, {
     id: 'myForm'
     ,title: 'Search Form'
     ,frame:true     
     ,waitMessage: 'Please wait.'
     //,labelWidth:80    
     ,initComponent: function() {    
         var config = {                 
            items: [{
                layout:{
                    type:'table',
                    columns:5
                },
                buttonAlign:'center',

                defaults:{
                    //width:150,
                    //bodyStyle:'padding:100px'
                    style:'margin-left:20px;'
                },               
                items:[//row 1
                       {                    
                            xtype: 'label',
                            name: 'dateLabel',
                            cls: 'f',
                            text: "Required:"                   
                        },
                        {
                            xtype: 'container',
                            layout: 'form',
                            items: {
                                xtype: 'datefield',
                                fieldLabel: "From Date",  
                                 value: yesterday,
                                width: 110,
                                id: 'date1'                                                   
                            }
                        }][1]
buttons: [{
                            text: 'Add More Criteria (max 10 items)',
                            id: "addBtn",                   
                            scope: this,
                            handler: function(){
                                alert('hi');
                                /*this.items.add({
                                     xtype : 'textfield',
                                     fieldLabel : 'Extra Field',
                                     name : 'yourName',
                                     id : 'yourName'
                                 }); */
                                this.insert(4,{
                                        xtype: 'textfield',
                                        id: 'input20',
                                        //hideLabel: true,
                                        width: 180,
                                        fieldLabel: 'hi'
                                    });
                                this.doLayout();
                            }
                }

form

2个回答

5
最简单的方法是在表单中使用一个fieldset来容纳所有的“行”,然后使用fieldset.add()向该
添加一行。

(您的“行”可以是另一个包含所有字段的

)


我也看到过一些将fieldContainer放在fieldSet中的情况,这样做是否正确?或者只需将另一个fieldset添加为行即可? - pm13
只需将另一个字段集添加为一行即可。 - Amol Katdare

1

动态表单字段生成似乎很明显,有许多示例和一些关于mootools等的博客,但在extjs世界中,我找不到一个可用的示例(可能是因为大多数人使用强大的Extjs网格)。我不得不为MedAlyser项目自己发明一个!现在我与你分享它。它可能存在错误和/或不完整,但我希望它能对你有所帮助。

function addressCounter(incr) {
    if (!this.no) {
        this.no = 0;
    } else {
        this.no = this.no + 1;
    };
};
var counter = new addressCounter();
console.log(counter.no);
//put below code inside your form  items       
{
    xtype: 'button',
    text: 'Add address ',
    id: 'addaddress',
    handler: function () {
        counter.no = counter.no + 1;
        console.log(counter.no);
        Ext.getCmp('patientaddress').add([{
            xtype: 'combo',
            store: 'AddressType',
            displayField: 'name',
            valueField: 'name',
            fieldLabel: 'Address Type',
            id: 'addresstype' + counter.no,
            name: "Patientaddress[addresstype][]",
            value: 'Home'
        }, {
            fieldLabel: 'zip',
            width: 160,
            maxLength: 10,
            enforceMaxLength: true,
            maskRe: /[\d\-]/,
            regex: /^\d{5}(\-\d{4})?$/,
            regexText: 'Must be in the format xxxxx or xxxxx-xxxx',
            name: "Patientaddress[zip][]",
            id: 'zip' + counter.no
        }, {
            fieldLabel: 'Address 1',
            name: "Patientaddress[address1][]",
            id: 'address1' + counter.no
        }, {
            fieldLabel: 'Address 2',
            name: "Patientaddress[address2][]",
            id: 'address2' + counter.no
        }, {
            fieldLabel: 'City',
            name: "Patientaddress[city][]",
            id: 'city' + counter.no
        }, {
            fieldLabel: 'State',
            name: "Patientaddress[state][]",
            id: 'state' + counter.no
        }, {
            xtype: 'combo',
            store: Ext.create('MA.store.Countries'),
            displayField: 'name',
            valueField: 'id',
            forceSelection: true,
            fieldLabel: 'Country',
            typeAhead: true,
            queryMode: 'local',
            name: "Patientaddress[country][]",
            id: 'country' + counter.no
        } // eof
        // countries;
        ,
        Ext.getCmp('addaddress'), {
            xtype: 'button',
            text: 'Remove address',
            id: 'removeaddress' + counter.no,
            handler: function (
            thisButton, eventObject) {

                activeRemoveButtonId = thisButton.getId().split('removeaddress')[1];

                console.log('activeRemoveButtonID:' + activeRemoveButtonId);
                Ext.getCmp('patientaddress').remove('address1' + activeRemoveButtonId);
                Ext.getCmp('patientaddress').remove('address2' + activeRemoveButtonId);
                Ext.getCmp('patientaddress').remove('city' + activeRemoveButtonId);
                Ext.getCmp('patientaddress').remove('state' + activeRemoveButtonId);
                Ext.getCmp('patientaddress').remove('country' + activeRemoveButtonId);
                Ext.getCmp('patientaddress').remove('removeaddress' + activeRemoveButtonId);
                Ext.getCmp('patientaddress').remove('addresstype' + activeRemoveButtonId);
                Ext.getCmp('patientaddress').remove('zip' + activeRemoveButtonId);

                Ext.getCmp('patientaddress').doLayout();
            }
        }]);
        Ext.getCmp('patientaddress').doLayout();

    } // eof function
}, // eof Add button

删除字段更加复杂,因为删除按钮需要精确知道要删除哪个字段。这段代码会根据您的要求正确地创建和删除字段。欢迎提出任何建议。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接