Learning Ext JS 3

A Floating window Más videos

Descripción del tema

The windows are very useful to display forms or information we need to show to the user, in this tutorial we will see important properties and methods to create windows in our applications.

Resources

Before you start with this tutorial you need to download the resources, this time you'll see only a HTML document that includes the Ext JS library and an empty JS file.

Packaging

"Packaging" the code is a very good practice when we're developing an application, we have talked about the reasons of doing it before.
//the namespace for this tutorial
Ext.ns('com.quizzpot.tutorial');

//the blank image
Ext.BLANK_IMAGE_URL = '../ext-2.2/resources/images/default/s.gif';

com.quizzpot.tutorial.Window = {
	init: function(){
		//code goes here
	}
}

Ext.onReady(com.quizzpot.tutorial.Window.init,com.quizzpot.tutorial.Window);
We will write the rest of the code inside of the function "init", by doing this we will avoid future problems.

A basic window

The component "Ext.Window" inherits the methods and properties from the component "Ext.Panel", this means that all the properties we use for the panels can be used in windows too, besides we can use some of other properties that can only be used in the component Window. To create a window we only need 3 configuration properties: the title, width and height.
var win = new Ext.Window({
	title: 'First window!', //the title of the window
	width: 300,
	height:250
});
//displaying the window to the user
win.show();
Ventana

Basic Window

The previous code shows how to create a very basic window, just remember that once we have create an instance of the component "Window" we need to invoke the method "show" to display the window in the screen.

Other configurations

By default the windows are displayed in the middle of the screen but we can modify their position by specifying the property x,y; also we can make the window modal, which makes the background to look like it's covered with a transparent gray layer allowing the user to pay the attention only to that window. We can also use the properties minimizable and maximizable.
var win = new Ext.Window({
	title: 'First window!',
	width: 300,
	height:250,
	minimizable: true, //show the minimize button
	maximizable: true, //show the maximize button
	modal: true, //set the Window to modal
	x: 100, //specify the left value of the window
	y: 100 //specify the top value of the window
});
//display the window in the screen
win.show();
Ventana modal

Minimize and Maximize button and modal window

Minimize a window

It's important to remember that we need to implement the functionality of the button minimize, because the windows don't have this defined by default; this gives us the opportunity to implement the functionality according to our needs, so let's start doing it by adding a "listener" to the event "minimize" like this:
var win = new Ext.Window({
   title: 'Quizzpot.com',
   width: 500,
   height:350,
   minimizable: true,
   maximizable: true,
   html: ''
});   
win.show();

//fire when the user clicks the minimize button
win.on('minimize',function(w){
   console.debug('minimizing...');
   w.collapse(); //collapse the window
});
The previous code creates a "listener" that will be triggered when the user clicks on the button minimize of the window, in this moment the function we defined will be called and the window is collapsed; just keep in mind that every developer decides what is going to happen in this function.

Content

To assign the content of the window we do the same thing we did for the panels:
  • Use the property "html" and writing the HTML directly.
  • Use the property "contentEl" we've asssigned the ID of an element of the DOM in order to be the content of the window
  • Use the property “autoLoad” or the method “load” to load the content using Ajax.
  • Use the property “items” and assigning an array of Ext JS components.
We have studied the previous configurations before, so I recommend you to read the previous tutorial in case you have some questions.

Loading external sites

If we need to display an external site like Google or Yahoo, we can use an iFrame to load the site. To do this we need to use the property "html" like this:
var win = new Ext.Window({
	title: 'First window!',
	width: 500,
	height:350,
	minimizable: true,
	maximizable: true,
	html: '<iframe src="http://www.quizzpot.com" style="width:100%;height:100%;border:none;"></iframe>'
});

var win2 = new Ext.Window({
	title: 'First window!',
	width: 500,
	height:350,
	minimizable: true,
	maximizable: true,
	html: '<iframe src="http://www.google.com" style="width:100%;height:100%;border:none;"></iframe>'
});

win.show();
win2.show();
Window with an external site

Window with an external site

Conclusions

In this tutorial you can see how to use the inheritance correctly, because a window is actually a panel with more functionalities like Drag, maximize and minimize buttons, also the window has the same methods of the panel to assign content. If you have any questions or suggestion please leave us a comment, also I invite to join our Forum to share your knowledge with the new community.

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.

¿Olvidaste tu contraseña?

10Comentarios

  • Avatar-9 lol 13/07/2009

    extjs no vale usen mejor mootols o dojo XD jquery si nooooooooo

    • Avatar-2 Crysfel 13/07/2009

      Apreciamos tu opinión, pero no podemos comparar Ext JS con mootools o JQuery ;) Dojo es el único que podría estar a la altura :D pero compara los demos oficiales de ambos productos y saca tus propias conclusiones. saludos y gracias por participar

      • Avatar-2 Ricky Baratan 19/02/2010

        I have a trouble when execute add button on the grid, when executed it the window appear but content on the formpanel binding to the window didn't show like (textfield, button, etc). But when i execute button outside the grid, nothing problem. Any clue regarding this issue? need your help. Thx.

        • Avatar-2 Crysfel 19/02/2010

          try to change the "layout" property (of the window) to "fit", something like this: new Ext.Window({ layout: "fit", width.... items: [form] }); good luck!

          • Avatar-10 DotNetWise 03/03/2010

            This page is causing two alerts/errors on IE8 http://screencast.com/t/ZjQ3OGZlMzYt

            • Avatar-3 Justin 10/03/2010

              minimize button works fine, but what if i want to again reopen the minimized window again to its original size (before minimizing)

              • Avatar-7 Rabaoui 10/03/2010

                hi everyone I have the same problem of justin :(

                • Avatar-2 Justin 12/03/2010

                  hi Rabaoui, i got the solution. win.expand();

                  • Avatar-4 Justin 12/03/2010

                    Yes, win = new Ext.Window. this is what i have used. you can use your object. expand() will do the job.

                    • Avatar-1 Jatan 19/02/2013

                      please provide images and css file for this modal box. When I download, I only got the js and html files. Thanks

                      Instructor del curso

                      Crysfel3

                      Autor: Crysfel Villa

                      Software engineer with more than 7 years of experience in web development.

                      Descarga Código Fuente Ver Demostración

                      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.