Académique Documents
Professionnel Documents
Culture Documents
Categories Series
JAVA S C R I P T & A JA X
Hey guys, Levi Hackwith here. Today, we're going to discuss version four
of the JavaScript framework, ExtJS. We're going to cover what it is, how
to install it, and then we're going to show off the power of ExtJS by
putting together a robust data grid using some sample data I've put
together.
Now that we've gone over some background information and got the
Framework uploaded to our website, let's start coding. Now as you can
see from the project files over here on the left, I've already set up a basic
folder structure for our application as well as create an index.html page
and a blank script.js JavaScript file. The HTML page is where we're going
to include all of the necessary scripts we uploaded earlier and the
JavaScript file is where we're going to put all of our application code.
Step 3 - Include the Necessary Files
01 <h
ht m l >
02 <h he a d >
03 <sst y l e >
04 </s st y l e >
05 <tti t l e ></tti t l e >
06 <mme t a charset=UTF-8">
07 </h he a d >
08 <b bo d y >
09 </b bo d y >
10 </hht m l >
Let's begin by opening up the index.html file. As you can see, I've
already set up a basic HTML page using the HTML5 doctype. Now I'm
going to tell you about the necessary files you'll need to create an ExtJS
application as well as how to include them.
01 <h
ht m l >
02 <h he a d >
03 <sst y l e >
04 @import url('extjs/resources/css/ext-all.css');
05 </s st y l e >
06 <tti t l e ></tti t l e >
07 <mme t a charset=UTF-8">
08 </h he a d >
09 <b bo d y >
10 </b bo d y >
11 </hht m l >
01 <h
ht m l >
02 <h he a d >
03 <ssc r i p t type = "text/javascript" src = "extjs/ext-al
04 <ssc r i p t type = "text/javascript" src = "script.js"
05 <sst y l e >
06 @import url('extjs/resources/css/ext-all.css');
07 </s st y l e >
08 <tti t l e ></tti t l e >
09 <mme t a charset=UTF-8">
10 </h he a d >
11 <b bo d y >
12 </b bo d y >
13 </hht m l >
Full Screencast
10:11
1 Ext.onReady(f
fu n c t i o n () {
2 });
Now, there's two things I want to point out about this piece of code: first
off, we're passing in an entire function into a method call (onReady).
This is what's called a callback: a function that gets called as soon as
another task is complete - in our case, the page finished loading.
Second, you'll notice that I prefaced this method call with "Ext" This is
called a namespace. A namespace is a way of containing variables and
methods into separate containers in order to prevent things like variable
collision. ExtJS relies heavily on namespaces in order to work properly.
In fact, every method call in ExtJS is contained in - at a minimum - one
namespace: Ext. You'll be exposed to more complex examples of
namespace usage as we progress through this tutorial.
Okay, now that we've set up our onReady method, let's declare our
dataGrid. Update your script.js file with the following:
1 Ext.onReady(f
fu n c t i o n () {
2 Ext.create('Ext.grid.Panel', {
3 });
4 });
1 Ext.onReady(f
fu n c t i o n () {
2 Ext.create('Ext.grid.Panel', {
3 store: Ext.create('Ext.data.Store', {})
4 });
5 })
A store is, for the most part, just a collection of records. A more
real-world example of this might be the contact app in your smartphone.
The part of the app that lets you see your contacts is the grid and the
part of the app that populates that list of contacts is the store.
01 Ext.onReady(f
fu n c t i o n () {
02 Ext.create('Ext.grid.Panel', {
03 store: Ext.create('Ext.data.Store', {
04 fields: [{
05 name: 'id',
06 type: 'int'
07 }, {
08 name: 'first_name',
09 type: 'string'
10 } ,{
11 name: 'last_name',
12 type: 'string'
13 }, {
14 name: 'dob',
15 type: 'date',
16 dateFormat: 'Y-m-d'
17 }]
18 })
19 });
20 });
As you can see, we've added a property called "fields" to our store
object. Fields are like the column headings in a spreadsheet. If each row
in the spreadsheet is one record, each column - or field - in the
spreadsheet, represents some property of that record. For our example
today, we're going to be making a data grid full of contacts so each
record in the store is going to have: an ID which simply provides a
unique identifier for each record, a first name, a last name, and a date of
birth. Now as you can see, for each field we've specified a 'name'
property and a 'type' property. These properties are pretty straight
forward: we're just telling our store what kind of field it is and what it
should be called. Now, when you get down to the dob - or date of birth -
field, you'll notice we've set a type of 'date' to signify a date field -
nothing really out of place there - but we've also added a 'dateFormat'
property. This property tells the store that the dob field will store its date
value in year, month, day format. This may seem odd now but it'll
become quite important once we start setting up the rest of the grid.
Step 7 - Fill the Store with Data
Now that we've set up our store and added some fields, let's go ahead
and populate it with data:
01 Ext.onReady(f
fu n c t i o n () {
02 Ext.create('Ext.grid.Panel', {
03 store: Ext.create('Ext.data.Store', {
04 fields: [{
05 name: 'id',
06 type: 'int'
07 }, {
08 name: 'first_name',
09 type: 'string'
10 } ,{
11 name: 'last_name',
12 type: 'string'
13 }, {
14 name: 'dob',
15 type: 'date',
16 dateFormat: 'Y-m-d'
17 }],
18 data: [{
19 'id': 1,
20 'first_name': 'John',
21 'last_name': 'Smith',
22 'dob': '1950-03-04'
23 }, {
24 id: 2,
25 'first_name': 'Jane',
26 'last_name': 'Doe',
27 'dob': '1960-07-24'
28 }]
29 })
30 });
31 });
Basically, what we've done here is fill our store with sample data using
the fields we defined earlier as a template. If you look close, you'll notice
that the property names in the data match the field names in the 'fields'
property of the store. This is called 'mapping'. We're mapping the data
from the data property, to its associated fields in the 'fields' property of
the store. Also note how we've made sure to make the data types of the
data match the data types of the store. If you get them mixed up things
may not load properly.
Step 8 Add Columns to the Grid
Okay, that's all we need to do to configure out grid's store so let's get
back to configuring the grid itself. The next thing we want to set up in
our grid is the columns. This is exactly what it sounds like: we're going
to be setting up what columns will be displayed when our grid loads.
01 Ext.onReady(ffu n c t i o n () {
02 Ext.create('Ext.grid.Panel', {
03 width: '30%',
04 store: Ext.create('Ext.data.Store', {
05 fields: [{
06 name: 'id',
07 type: 'int'
08 }, {
09 name: 'first_name',
10 type: 'string'
11 } ,{
12 name: 'last_name',
13 type: 'string'
14 }, {
15 name: 'dob',
16 type: 'date',
17 dateFormat: 'Y-m-d'
18 }],
19 data: [{
20 'id': 1,
21 'first_name': 'John',
22 'last_name': 'Smith',
23 'dob': '1950-03-04'
24 }, {
25 id: 2,
26 'first_name': 'Jane',
27 'last_name': 'Doe',
28 'dob': '1960-07-24'
29 }]
30 }),
31 columns: [{
32 header: 'ID',
33 dataIndex: 'id'
34 }, {
35 header: 'First Name',
36 dataIndex: 'first_name'
37 }, {
38 header: 'Last Name',
39 dataIndex: 'last_name'
40 }, {
41 header: 'Date of Birth',
42 dataIndex: 'dob',
43 format: 'm/d/Y',
44 xtype: 'datecolumn'
45 }]
46 });
47 });
Just like we did before, we've declared the property - "columns" - and
passed in an array of objects, each object representing a single column
in the grid. Notice how each column contains at least two properties:
'header' and 'dataIndex'. Header specifies what gets displayed as the
column header in the grid (what the user will see); dataIndex maps that
column to a particular field in the store. And that's it! That's all you need
to do to build a data grid.
1 ...
2 v a r grid = Ext.create('Ext.grid.Panel', {
3 ...
All we're saying here is "Grid, render to the body of the HTML document"
- Pretty straight forward. Go ahead and refresh our index.html file and
see how we did. As you can see, in under 30 minutes we've created a
robust data grid that has all these features: we can show and hide
columns, we can sort the data and we can rearrange the columns. That's
the power of ExtJS!
Advertisement
Review
In Review, we discussed what ExtJS is, the difference between a library
and a framework, how to download and install ExtJS, and we discussed
how to define and configure an ExtJS object - in this case a data grid.
Di�culty:
Intermediate
Length:
Short
Tagged with:
Download Attachment
N/A
Advertisement
Related Posts
Advertisement
Tutorials
Courses
eBooks
Jobs
Blog
Follow Us
Subscribe to Blog
Follow us on Twitter
Be a fan on Facebook
Circle us on Google+
RSS Feed
About Blog
Pricing FAQ
Terms of Use