Constructing a complex form layout with Ext JS 4
In previous releases of Ext JS complicated form layouts were quite difficult to achieve. This was due to the nature of the FormLayout, which was required to display labels and error messages correctly, and how it had to be combined with other nested layouts.
title: ‘Support Ticket Request’,
width: 650,
height: 500,
renderTo: Ext.getBody(),
style: ‘margin: 50px’,
items: [] });
[/javascript]
title: ‘Support Ticket Request’,
width: 650,
height: 500,
renderTo: Ext.getBody(),
style: ‘margin: 50px’,
items: [{
xtype: 'container',
layout: 'hbox',
items: [{
xtype: 'textfield',
fieldLabel: 'First Name',
name: 'FirstName',
labelAlign: 'top',
cls: 'field-margin',
flex: 1
}, {
xtype: 'textfield',
fieldLabel: 'Last Name',
name: 'LastName',
labelAlign: 'top',
cls: 'field-margin',
flex: 1
}] }] });
[/javascript]
Ext JS 4 takes a different approach and utilizes the Ext.form.Labelable mixin, which allows form fields to be decorated with labels and error messages without requiring a specific layout to be applied to the container. This means we can combine all of the layout types the framework has to offer (which are discussed in detail in Chapter 3, Laying Out your Components) without having to overnest components in order to satisfy the form field’s layout requirements.
We will describe how to create a complex form using multiple, nested layouts and demonstrate how easy it is to get a form to look exactly as we want. Our example will take the structure of a Support Ticket Request form and, once we are finished, it will look like the following screenshot:
How to do it…
1. We start this recipe by creating a simple form panel that will contain all of the layout containers and their fields:
[javascript] var formPanel = Ext.create(‘Ext.form.Panel’, {title: ‘Support Ticket Request’,
width: 650,
height: 500,
renderTo: Ext.getBody(),
style: ‘margin: 50px’,
items: [] });
[/javascript]
2. Now, we will create our first set of fields—the FirstName and LastName fields. These will be wrapped in an Ext.container.Container component, which is given an hbox layout so our fields appear next to each other on one line:
[javascript] var formPanel = Ext.create(‘Ext.form.Panel’, {title: ‘Support Ticket Request’,
width: 650,
height: 500,
renderTo: Ext.getBody(),
style: ‘margin: 50px’,
items: [{
xtype: 'container',
layout: 'hbox',
items: [{
xtype: 'textfield',
fieldLabel: 'First Name',
name: 'FirstName',
labelAlign: 'top',
cls: 'field-margin',
flex: 1
}, {
xtype: 'textfield',
fieldLabel: 'Last Name',
name: 'LastName',
labelAlign: 'top',
cls: 'field-margin',
flex: 1
}] }] });
[/javascript]
3. We have added a CSS class (field-margin) to each field, to provide some spacing between them. We can now add this style inside
Comments
Post a Comment