Background:
Ok let’s assume for a minute that you have a tab panel with four tabs each containing a form. You have placed this panel inside of a window, and the goal is to have one ‘save’ button on the bottom of the window that will save all of the forms simultaneously so the user does not have to remember to save after changing data on a tab before moving to the next. This is a common and straight forward task, and I am sure that it comes up in the real world often; I’ve used this layout multiple times myself.
The Problem:
You want to read the form values and consolidate them into one call to your backend database, however, then form elements in the tab panels behind the default panel have not been rendered to the dom, therefore, you cannot access them via getValues().
You want to set the values in each form; however, you cannot set values for items that do not exist.
The Solution:
The code below will construct a button that can be placed at the ‘Window’ level and will try to get the values of all child forms contained in a tab panel inside the window.
buttons:[
{
text : 'Save',
listeners : {
click : function() {
// The first form is always rendered!
var first_form_values = first_form.getForm().getValues(false),
// This checks the form first to see if they have been rendered
second_form_values = !second_form.rendered ? '' : second_form.getForm().getValues(false),
third_form_values = !third_form.rendered ? '' : third_form.getForm().getValues(false),
fourth_form_values = !fourth_form.rendered ? '' : fourth_form.getForm().getValues(false);
Ext.Ajax.request({
url:'your_url.php?' +
// You need urlEncode as getValues passes
// back an object!
'&' + Ext.urlEncode(first_form_values) +
'&' + Ext.urlEncode(second_form_values) +
'&' + Ext.urlEncode(third_form_values) +
'&' + Ext.urlEncode(fourth_form_values),
method:'POST',
success:function () {
msg('','Your changes have been saved successfully!');
},
failure:function () {
msg('uh-oh','Your changes have not been saved!');
}
});
}}
}
]
That’s all there is to sending your data values from multiple forms to the server in one ajax post, but what about getting values from the server and updating your forms that reside in the tab panel?
Luckily, when an element is in the tab panel you can add an ‘activate‘ listener that is waiting for the tab and form to become visible, and the best part is it is fired every time you click on the tab, which would be useful as afterrender is called once, and that’s when the component is first rendered (as the name suggests ).
The code below will load values to your form and must be placed at the formPanel element level.
listeners :{
'activate': function(e){
working_status_form.getForm().load({
url: '/getData.php',
failure: function(form, action) {
Ext.Msg.alert('uh-oh','Your data has not been loaded.');
}
});
}
},