What is a panel? What’s the functionality? How do you create one? Más videos
Descripción del tema
Resources
Before we begin this tutorial you need to download the resources, unzip them and copy the files to the Web server we installed in the first chapter of this course, inside of the folder "course", so create a folder named "panels" and place the resources there.What is a Panel?
A panel is a container that has specific functionality and structural components that make it the perfect building block for application-oriented user interfaces. We can add toolbars, buttons at the bottom or make the panel collapsible. The panels can be assign easily to another container.Create the first panel
Is very easy to create a panel, all you need to do is write the following code:var main = new Ext.Panel({ title: 'My first panel', //panel's title width:250, //panel's width height:300, //panel's height renderTo: 'frame', //the element where the panel is going to be inserted html: 'Nothing important just dummy text' //panel's content });The previous code creates a panel with the title "My first panel", with the dimensions of 250 x 300 pixels, this panel will be inserted inside the element "frame" that we have in the HTML document, the content has the text "Nothing important just dummy text."
Panel's example
var main = new Ext.Panel({ title: 'My first panel', width:250, height:300, html: 'Nothing important just dummy text' }); //we use the method "render" to display the panel in the screen main.render('frame');As you can see, the object "Panel" has a method capable to render or insert the HTML we need so the user can look at the panel in the screen, this method is called "render" and accepts as a parameter the "id" of the node where the panel is going to be inserted.
Panel's content
The content of a panel can be assigned in four different ways, we have seen one of them in the previous codes, by using the property "html" we can specify the content we need to add. The second option we have is defining with the property "contentEl" the "id" of the element we need to in the panel, is necessary to have this element already in the DOM so it can be inserted in the panel. Let's remove the comment of the HTML document we have in the resources, after doing that you need to write the following code:var main = new Ext.Panel({ title: 'My first panel', width:250, height:300, contentEl: 'content' //using an element from the DOM as the content }); main.render('frame');
Content loading from an element from the DOM
var main = new Ext.Panel({ title: 'My first panel', width:250, height:300 }); main.render('frame'); //using Ajax to insert the content main.load('panel.php');
Content loaded through Ajax
var main = new Ext.Panel({ title: 'Mi primer panel', width:250, height:300, autoLoad: 'panel.php' //<--- The content will be retrieve from here automatically }); main.render('frame');The fourth option is to insert other Ext JS components, it can be Panels, Tabs, trees, forms, etc. Before we do this we need to create the component we will need to add and then add it with the property "items", which is an array of components.
//creating the inner panel var panel1 = new Ext.Panel({ title: 'Users', html: 'The content', bodyStyle: 'padding:10px;', //we can assign styles to the main div height:200, border: false //we can remove the border of the panel }); //this the main panel that will have other panels inside var main = new Ext.Panel({ title: 'My first panel', width:250, height:600, items: [panel1] //assigning the components }); //rendering all the created panels main.render('frame');In the previous code we created a panel and inserted another panel inside, this is very common when we develop layouts with Ext JS. It's important to remember that we we call the method "render" in the main panel, all the components the panels has will be rendered automatically.
Components inside of a panel
Collapsing panels
One of the characteristics of the panels is that they can be collapsed by clicking in a button displayed at the right upper side of the panel, all we need to do is to set to "true" the property "collapsible".//creating the inner panel var panel1 = new Ext.Panel({ title: 'Users', html: 'The content', bodyStyle: 'padding:10px;', height:200, border: false, collapsible: true //we use this property to collapse the panel }); //… we don't need to show the rest of the code, since we didn't change anything there.
Collapsible Panel
//creating the inner panel var panel1 = new Ext.Panel({ title: 'Users', html: 'The content, bodyStyle: 'padding:10px;', height:200, border: false, collapsible: true, titleCollapse: true //now the panel will collapse when we click on the title }); //… we don't need to show the rest of the code, since we didn't change anything there.
Adding an icon in the panel's title
There may be times that we need to add icons in the title's bar to improve the "look and feel" of the application, all we need to do is define a CSS class that will place the image (icon) as a background..users{ background: url(icons/users.png) 0px 0px no-repeat !important; }Once we have defined the CSS class, we need to assigned this class to the panel with the property "iconCls" like this:
//creating the inner panel var panel1 = new Ext.Panel({ title: 'Users', html: 'The content', bodyStyle: 'padding:10px;', height:200, border: false, collapsible: true, titleCollapse: true, iconCls: 'users' //this will add the icon we defined in the CSS class }); //... we don't need to show the rest of the code, since we didn't change anything there.
Panel with a personalized icon
The property “defaults”
Let's create two more panels exactly the same as the previous panel, we will only change the title and the icon.var panel1 = new Ext.Panel({ title: 'Users', iconCls: 'users', collapsible:true, border: false, bodyStyle: 'padding:10px;', titleCollapse: true, height:200 }); var panel2 = new Ext.Panel({ title: 'Reports', iconCls: 'reports', collapsible:true, border: false, bodyStyle: 'padding:10px;', titleCollapse: true, height:200 }); var panel3 = new Ext.Panel({ title: 'Documents', iconCls: 'documents', collapsible:true, border: false, bodyStyle: 'padding:10px;', titleCollapse: true, height:200 }); var main = new Ext.Panel({ title: 'My first panel', width:250, height:600, items: [panel1,panel2,panel3] }); main.render('frame');Maybe you have noticed already that there's a bunch of code we repeat when we create the first three panels, especially five properties. Ext JS allow us to define properties by default for the components that will use the same properties, in this way we can avoid writing the same code every time. With the property "defaults" we can define the properties we want to be applied on the components that the parent component has.
var panel1 = new Ext.Panel({ title: 'Users', iconCls: 'users' }); var panel2 = new Ext.Panel({ title: 'Reports', iconCls: 'reports' }); var panel3 = new Ext.Panel({ title: 'Documents', iconCls: 'documents' }); var main = new Ext.Panel({ title: 'My first panel', width:250, height:600, defaults: { // with this property we avoid... collapsible:true, //code duplicity... border: false, // and all that... bodyStyle: 'padding:10px;', // properties... titleCollapse: true, // will be added to... height:200 //all the inner panels }, items: [panel1,panel2,panel3] }); main.render('frame');
Different panels inside of a panel container
Loading the information
We have seen how to load the content of the panels using Ajax, what we're going to do now is to pass parameters so we can receive the correct information, to do this we need to pass to the method "load" an object with the "url", the method we're going to use and the parameters we will need.panel1.load({ url: 'panel.php', method: 'GET', params: {data:'users'} }); panel2.load({ url: 'panel.php', method: 'GET', params: {data:'report'} }); panel3.load({ url: 'panel.php', method: 'GET', params: {data:'documents'} });All the requests are made to the same PHP file, which receives a parameter and depending on its content, it will return the correct information.
Final example
Conclusions
In this tutorial we learned how to use the component Panel, also we saw some of the most common properties and methods, so I recommend you to read the API and play a bit more with this component. If you have any suggestions or questions please leave a comment or subscribe to the Forums and I will gladly answer you as soon as I can.Te gustaría recibir más tutoriales como este en tu correo?
Este tutorial pertenece al curso Learning Ext JS 3, te recomiendo revises el resto de los tutoriales ya que están en secuencia de menor a mayor complejidad.
Si deseas recibir más tutoriales como este en tu correo te recomiendo registrarte al curso, si ya eres miembro solo identifícate y registrate al curso, si no eres miembro te puedes registrar gratuitamente!
Si no gustas registrarte en este momento no es necesario! Aún así puedes recibir los nuevos tutoriales en tu correo! Jamás te enviaremos Spam y puedes cancelar tu suscripción en cualquier momento.
Regístrate a este curso
Este tutorial pertenece al curso Learning Ext JS 3, revisa todos los tutoriales que tenemos en este mismo curso ya que están en secuencia y van de lo más sencillo a lo más complicado.
Tendrás acceso a descargar los videos, códigos y material adicional.
Podrás resolver los ejercicios incluidos en el curso así como los Quizzes.
Llevarás un registro de tu avance.
Se el primero en comentar!