1

I am adding a textfield dynamically when i click add button, after that i need to get the values what i added dynamically while i click save button. its in extjs2.0 itself.

This is the code what i am working onenter image description here

var settingsEmailPanel = new Ext.FormPanel({
                                labelWidth: 75, // label settings here cascade unless overridden
                                url:'save-form.php',
                                frame:true,
                                id:'emailForm',
                                title: 'Email Servers',
                                bodyStyle:'padding:5px 5px 0',
                                width: 350,
                                defaults: {width: 230},
                                defaultType: 'checkbox',

                                items: [{
                                        boxLabel: 'Server 1',
                                        hideLabel: true,
                                        name: 'server1'
                                    },{
                                        boxLabel: 'Server 2',
                                        hideLabel: true,
                                        name: 'server2'
                                    },{
                                        boxLabel: 'Server 3',
                                        hideLabel: true,
                                        name: 'server3'
                                    }, {
                                        boxLabel: 'Server 4',
                                        hideLabel: true,
                                        name: 'server4'
                                    }
                                ],

                                buttons: [{
                                    text: 'Add',
                                    handler: function () {
                                    settingsEmailPanel.add({
                                            xtype: 'textfield',
                                            fieldLabel: 'New ServerName',
                                            name: 'newServer',
                                            allowBlank:false
                                        });
                                    settingsEmailPanel.doLayout();
                                    }
                                },{
                                    text: 'Save',
                                    handler: function () {
                                        alert("Save clicked");
                                        console.log(Ext.getCmp("emailForm").getForm().findField("newServer").getValue());
                                    }
                                }]
                            });
Bhaskara Arani
  • 1,556
  • 1
  • 26
  • 44

2 Answers2

3

Here what I am trying to do is onclick of save I need to query each element of form and if element xtype is textfield then getting value each by each and displaying that.

Code for save button is :

{
    text: 'Save',
    handler: function() {
        var settingsEmailPanel = this.up().up();
        var settingsEmailPanelItem = settingsEmailPanel.items.items;
        for (var i = 0; i < settingsEmailPanelItem.length; i++) {
            if (settingsEmailPanelItem[i].xtype == "textfield") {
                var Value = settingsEmailPanelItem[i].getValue();
                alert(Value)
            }
        }
        // alert("Save clicked");
        //console.log(Ext.getCmp("emailForm").getForm().findField("newServer").getValue());
    }
}

I created a fiddler for you which you can check here.

Fiddle

Note : I can not see fiddle work for ExtJs 2 so I did in ExtJS 4. For making work in extJS 2 you need to assign uniqe id to each textfield and then retrive the value I suppose. The above answer will work from ExtJS 4 and above. if you make a fiddle for ExtJS 2 and I will try further more.

UDID
  • 2,350
  • 3
  • 16
  • 31
1

Just add them to an array every time you create one so you can retrieve them later in your save button handler :

var textFields = [];    
/*
 *  [...]
 */
buttons: [{
    text: 'Add',
    handler: function () {
        var textField = Ext.create('Ext.form.field.Text', {
            xtype: 'textfield',
            // ...
        });
        textFields.push(textFields);
        settingsEmailPanel.add(textField);
        settingsEmailPanel.doLayout();
    }
},{
    text: 'Save',
    handler: function () {
        for(var i=0; i<textFields.length; i++){
            console.log(textFields[i].getValue());
        }
    }
}]
Ludovic Feltz
  • 11,416
  • 4
  • 47
  • 63