Beruflich Dokumente
Kultur Dokumente
com
AN USER GUIDE
TO
Ext JS and AJAX
www.Fullinterview.com 1
www.Fullinterview.com
Reviewed Thribhuvan
Krishnamurthy
Project Lead Sony Music
by
www.Fullinterview.com 2
www.Fullinterview.com
TABLE OF CONTENTS
INTRODUCTION 4
WHAT IS EXT JS?......................................................................................................................................................4
WHAT IS AJAX?........................................................................................................................................................4
EXT JS BROWSER SUPPORT..............................................................................................................................................5
ADVANTAGES OF USING EXT JS:......................................................................................................................................5
GETTING STARTED 5
GETTING EXT:............................................................................................................................................................5
WHERE TO PUT EXT:....................................................................................................................................................5
HOW TO INCLUDE EXT:.................................................................................................................................................6
INITIAL EXAMPLE:.........................................................................................................................................................7
COMPONENTS OF EXT JS 11
THE CORE COMPONENTS:..............................................................................................................................................11
FORM:...................................................................................................................................................................11
BUTTONS, MENUS, AND TOOLBARS..................................................................................................................................13
DISPLAYING DATA WITH GRIDS.......................................................................................................................................15
17
REUSE BY EXTENDING EXT JS 18
OOPS WITH EXT JS....................................................................................................................................................18
INHERITANCE............................................................................................................................................................18
PACKAGES, CLASSES AND NAMESPACES...............................................................................................................................18
CREATING A CUSTOM NAMESPACE.....................................................................................................................................19
CORE AJAX 19
CREATING A XMLHTTPREQUEST........................................................................................................................................19
MAKING AJAX REQUEST FROM EXT JS...............................................................................................................................20
DOWNLOAD LINKS: 21
ACKNOWLEDGEMENT: 21
AUTHORS PROFILE: 22
www.Fullinterview.com 3
www.Fullinterview.com
Introduction
What is AJAX?
The term AJAX (Asynchronous JavaScript and XML) is an overly-
complicated acronym for saying that processes can take place in the
background while the user is performing other tasks. A user could be filling
out a form while a grid of data is loading—both can happen at the same time,
with no waiting around for the page to reload.
www.Fullinterview.com 4
www.Fullinterview.com
Getting Started
Getting Ext:
You can download the ext SDK (Software Development Kit), from
http://www.extjs.com/products/extjs/download.php. This contains a ton of
useful examples and the API reference. Most importantly, it contains the
resources that Ext needs to run properly.
www.Fullinterview.com 5
www.Fullinterview.com
• adapter: Files that allow you to use other libraries alongside Ext
• build: Files that can be used to custom-build an ext-all.js
• Docs: The documentation center (this will only work when run on a
web server)
• examples: Plenty of amazing and insightful examples
• resources: Dependencies of the Ext library, such as CSS and images
• source: The complete source code for Ext
When you're ready to host your page on a web server, the adapter and resources
folders will need to be uploaded to the server.
www.Fullinterview.com 6
www.Fullinterview.com
<html>
<head>
<title>Hello World</title>
<link rel="stylesheet" type="text/css" href="JS/ext-2.1/resources/css/ext-
all.css" />
<script src="JS/ext-2.1/adapter/ext/ext-base.js"></script>
<script src="JS/ext-2.1/ext-all-debug.js"></script>
</head>
<body>
</body>
</html>
The path to the EXT files must be correct and is relative to the location of our
HTML file. These files must be included in the following order:
I have included the following three files that Ext requires to run in our page:
• ext-all.css: A style sheet file that controls the look and feel of Ext
widgets. This file must always be included as-is, with no modifications.
Any changes to the CSS in this file would break future upgrades. If the
look and feel of Ext needs to be adjusted, another style sheet
containing the overrides should be included after the ext-all.css file.
• ext-base.js: This file provides the core functionality of Ext. It's the
engine of the Ext car. This is the file that I would change if I wanted to
use another library, such as jQuery, along with Ext.
• ext-all-debug.js/ext-all.js: All of the widgets live in this file. The
debug version is used for development, and then swapped out for the
non-debug version for production.
Once these files are in place, I can start to actually use the Ext library and
have some fun.
Initial Example:
We can run some Ext code by adding a script section in the head of our
document, right after here the Ext library has been included. Our example
will bring up an Ext style alert dialog:
www.Fullinterview.com 7
www.Fullinterview.com
<html>
<head>
<title>Hello World</title>
<script src="JS/ext-2.1/adapter/ext/ext-base.js"></script>
<script src="JS/ext-2.1/ext-all-debug.js"></script>
<script>
Ext.onReady(function(){
});
</script>
</head>
<body>
</body>
</html>
www.Fullinterview.com 8
www.Fullinterview.com
Let's take a closer look at the two core Ext functions we have just used:
• Ext.MessageBox
• Ext.Msg
• Ext.Msg.progress
• Ext.Msg.show
• Ext.Msg.updateProgress
• Msg.alert
• Msg.prompt
www.Fullinterview.com 9
www.Fullinterview.com
Some other ways to display a message box is by displaying more than one
button as given below.
<html>
<head>
<title>Hello World</title>
<link rel="stylesheet" type="text/css" href="JS/ext-
2.1/resources/css/ext-all.css" />
<script src="JS/ext-2.1/adapter/ext/ext-base.js"></script>
<script src="JS/ext-2.1/ext-all-debug.js"></script>
<script>
Ext.onReady(function(){
Ext.Msg.show({
title: 'Hello World',
msg: 'Are you able to see the three button?',
buttons: { yes: true, no: true, cancel:
true}
});
});
</script>
</head>
<body>
</body>
</html>
Have a look at the screen shot below. This has alert message with three
buttons present.
www.Fullinterview.com 10
www.Fullinterview.com
Components of ext js
The core components:
• Form
• Buttons, Menus, and Toolbars
• Displaying Data with Grids
Form:
Forms are similar to the HTML forms that we use, without the usability
restrictions and boring user interface. Here are some of the core form
components that you should become familiar with:
• Ext.form.FormPanel
• Ext.form.Field
www.Fullinterview.com 11
www.Fullinterview.com
<html>
<head>
<title>Hello World</title>
<link rel="stylesheet" type="text/css" href="JS/ext-
2.1/resources/css/ext-all.css" />
<script src="JS/ext-2.1/adapter/ext/ext-base.js"></script>
<script src="JS/ext-2.1/ext-all-debug.js"></script>
<script>
Ext.onReady(function(){
var user_details = new Ext.FormPanel({
renderTo: document.body,
frame: true,
title: 'User Details',
width: 250,
items: [{
xtype: 'textfield',
fieldLabel: 'First Name',
name: 'firstname'
},
{
xtype: 'textfield',
fieldLabel: 'Last Name',
name: 'lastname'
},
{
xtype: 'datefield',
fieldLabel: 'Date of Birth',
name: 'dob'
}
]
});
});
</script>
</head>
<body>
</body>
</html>
www.Fullinterview.com 12
www.Fullinterview.com
A form can contain any of these following components given below just nee
to change the xtype property.
• textfield
• timefield
• numberfield
• datefield
• combo
• textarea
• HtmlEditor
• CheckboxGroups
• RadioGroup
Each component of form has its own property like allowedblank, vtype etc.
For more details http://www.extjs.com/deploy/dev/docs/
www.Fullinterview.com 13
www.Fullinterview.com
The toolbar consist of button, split button and menu. In the below example
we have shown all the three type.
<script type="text/javascript">
Ext.onReady(function(){
new Ext.Toolbar({
renderTo: document.body,
items: [{
xtype: 'tbbutton',
text: 'Button'
},{
xtype: 'tbbutton',
text: 'Menu Button',
menu: [{
text: 'Better'
},{
text: 'Good'
},{
text: 'Best'
}]
},{
xtype: 'tbsplit',
text: 'Split Button',
menu: [{
text: 'Item One'
},{
text: 'Item Two'
},{
text: 'Item Three'
}]
}]
});
});
</script>
www.Fullinterview.com 14
www.Fullinterview.com
First save this file with the name USERDETALS.xml. Note: To run this example
you should have a web server so that the data gets rendered in the grid.
<?xml version="1.0" encoding="UTF-8"?>
<dataset>
<row>
<id>1</id>
<firstname>Kalim</firstname>
<lastname>Chandsha</lastname>
<mobilenumber>9880227861</mobilenumber>
</row>
<row>
<id>2</id>
<firstname>Thribhuvan</firstname>
<lastname>Krishnamurthy</lastname>
<mobilenumber>9886178850</mobilenumber>
</row>
</dataset>
www.Fullinterview.com 15
www.Fullinterview.com
Ext.onReady(function(){
var store = new Ext.data.Store({
proxy: new Ext.data.HttpProxy({url: 'USERDETAILS.xml'}),
renderTo: document.body,
reader: new Ext.data.XmlReader({
record:'row',
id:'id'
},['id', 'firstname', 'lastname', 'mobilenumber'])
});
store.load();
});
</script>
www.Fullinterview.com 16
www.Fullinterview.com
The similar way we can go with JSON data have a look below.
{ success:true,
rows:[
{
"id":"1",
"firstname":"Kalim",
"lastname":"Chandsha",
"mobilenumber":"9880227861",
},{
"id":"3",
"firstname":"Thribhuvan",
"lastname":"Krishnamurthy",
"mobilenumber":"9886178850",
}
]
}
This JSON data can be brought from a server dynamically. We have different
event handlers for the row and column actions.
www.Fullinterview.com 17
www.Fullinterview.com
Inheritance
To gain a full understanding of what we need to do, we have to
understand one of the key concepts of object-oriented programming—
inheritance. As we write our own components, these components will usually
inherit some other component of Ext JS, extending that component's
functionality by providing our own properties and overriding the component's
existing methods. We are creating new classes from an existing Ext JS class
to determine whether the method that has been called is that of our new
class, the method of the extended (parent) class, or a combination of the two.
Packages
A package is a collection of classes that share something in common.
For instance, the Ext.data package is a collection of classes for dealing with
data, such as the different types of data Stores, Readers, and Records. The
Ext.grid package is a collection of classes for the various grid objects,
including all of the different grid types and selection models. Likewise, the
Ext.form package contains classes for building forms, to include all of the
classes of the different field types.
Classes
A class is what we call a specific JavaScript object, defining that
object's attributes and methods. Going back to our previous examples,
Person and SalesPerson would both be written as class objects, and both
would probably be part of the same package.
Namespaces
www.Fullinterview.com 18
www.Fullinterview.com
Classes are a part of packages, and packages generally have their own
namespace. Namespaces are containers of logically grouped packages and
class objects. As an example, the Ext JS library falls into the Ext namespace.
Forms within Ext JS fall into the Ext.forms namespace, where forms is a
package of the various classes used to make up forms. It's a hierarchal
relationship, using dot notation to separate the namespace from the package
from the class. Variables from one namespace must be passed into another
namespace, so applying a namespace helps to encapsulate information
within itself. Ext.grid, Ext.form, and Ext.data are all custom namespaces.
Ext.namespace('Ext.ux');
Core Ajax
Creating a xmlhttprequest
Here I will create a core Ajax xmlhttprequest to make a request to the server.
<script type="text/javascript">
function ajaxFunction()
{
var xmlhttp;
if (window.XMLHttpRequest)
{
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else if (window.ActiveXObject)
{
// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
else
{
alert("Your browser does not support XMLHTTP!");
}
xmlhttp.onreadystatechange=function()
www.Fullinterview.com 19
www.Fullinterview.com
{
if(xmlhttp.readyState==4)
{
//get the xmlhttp.responseText;
}
}
xmlhttp.open("GET","UserDetailsServlet",true);
xmlhttp.send(null);
}
</script>
www.Fullinterview.com 20
www.Fullinterview.com
Download Links:
Ext js http://www.extjs.com
Acknowledgement:
We acknowledge:
www.Fullinterview.com 21
www.Fullinterview.com
Authors Profile:
Employee ID 171335
www.Fullinterview.com 22