Summary of usage of ExtJS (1)--Configuration and table controls use

Source: Internet
Author: User

ExtJS when a set of AJAX control, I think it is the best I have seen the most beautiful JS control library, so very learning and use value, if you have not touched the ExtJS or not interested in it, then there is no need to look down, ExtJS is a complete set of RIA solutions, Also because of the complete function of Ext-all.js has more than 400 K, because it is based on the functions of JS and CSS, the client machine performance also has certain requirements, that is, do not support the IE6 version below. If your project has a strict limit on the response time of a Web page, or if the client operating system is too old, be sure not to choose ExtJS.

This article is mainly about the download and configuration of ExtJS and some simple ways to use it. Currently the latest version is 3.0, but this article mainly introduces the 2.2 version. First, ExtJS download and configuration

1, download Address: www.extjs.com/(This is the official website, you can choose their favorite version of the download)

2, the configuration process, assuming the downloaded directory for ext, we have in the directory to build our own directory Myexample (the directory used to store your own written code), the configuration process is as follows:

(1) Create a new paging file helloworld.html

(2) Add the following code between

<link rel= "stylesheet" type= text/css "href=". /resources/css/ext-all.css "/> <script type=" Text/javascript "src=". /adapter/ext/ext-base.js "></script> <script type=" Text/javascript "src=". /ext-all.js "></script> <script type=" Text/javascript "> Ext.onready (Function () {Ext.MessageBox.alert (' HelloWorld ', ' Hello World '); }) </script>

(3) Note here <script></script> cannot replace with </script>

(4) JS Import order do not change

(3) If a HelloWorld dialog box pops up, the configuration is successful. second, the use of Grid control table

The tables in ext are very powerful, including the ability to sort, cache, drag, hide a column, automatically display line numbers, column totals, cell editing, and so on. Let's start by introducing how to make a simple grid.

1, create the table column information:

var cm=new Ext.grid.ColumnModel ([{header: ' number ', Dataindex: ' ID '}, {header: ' name ', Dataindex: ' names '}, {header: ' description ', Dataindex: ' DESN '}]);

2, add data information:

var data=[[' 1 ', ' name1 ', ' DESN1 '], [' 2 ', ' name1 ', ' DESN1 '], [' 3 ', ' name1 ', ' DESN1 '], [' 4 ', ' name1 ', ' DESN1 '], [' 5 ', ' name1 ', ' DESN1 ']];

3. Create Data storage objects:

var ds=new Ext.data.Store ({proxy:new Ext.data.MemoryProxy (data), Reader:new Ext.data.ArrayReader ({},[{name: ' ID '}, { Name: ' Name '}, {name: ' DESN '}]}); Ds.load ()//This is quite important

4, the table column model defined well, the original data and data conversion has been completed, the rest of the only need to assemble them together, our grid was created successfully.

var grid=new Ext.grid.GridPanel ({renderto: "Grid", Store:ds, height:600, cm:cm});

5. Note: Ext.grid.Grid's Renderto attribute indicates where EXT will render the table, so there should be a <div id= ' grid ' ></div> corresponding to it in HTML.

6, all the code list is as follows (has passed the test):

<%@ Page language= "C #" autoeventwireup= "true" codefile= "Grid.aspx.cs" inherits= "Ext_example_grid"%> <! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">

The experimental effect diagram is shown in Figure 1

Figure 11 A simple grid three, the Table control grid function detailed

The second section briefly describes how to create a simple grid, which is a detailed analysis of the functionality of the grid. 3.1 Partial attribute function

1, by default, the grid can drag and drop columns, you can also change the width of the column, if you want to disable these two features, when the grid object is defined, respectively, set Enablecolumnmove and Enablecolumnresize to false.

2, if want to show zebra effect, can add striperows:true

3, Grid also supports a reading of data masks and hints, set properties Loadmask:true, Store.load () before the completion of the show "Loading ..." 3.2 autonomously determine the width of each column

1, if you want to define width, just set the column's Width property, as shown in the following code. The effect figure is shown in Figure 2.

var cm=new Ext.grid.ColumnModel ([{header: ' numbered ', dataindex: ' ID ', width:60}, {header: ' name ', Dataindex: ' Names ', width:180}, {header: ' description ', dataindex: ' Desn ', width:200}];

Figure 2 Customizing the width of each column

2, so you need to calculate the width of each column, if you want to allow each column automatically fill grid, only need to viewconfig in the Forcefit can. After using Forcefit, the grid will be proportionally distributed according to the width you set in cm, which is very intelligent. The implementation code is as follows:

var grid=new Ext.grid.GridPanel ({renderto: "grid", striperows:true,//zebra effect loadmask:true, Store:ds, height:600, CM:CM, viewconfig:{forcefit:true});

3, we can also consider Autoexpandcolumn, which allows the width of the specified column to automatically extend, so as to fill the entire table. The code is as follows

var grid=new Ext.grid.GridPanel ({renderto: "grid", striperows:true,//zebra effect loadmask:true, Store:ds, height:600, CM:CM, Autoexpandcolumn: ' DESN '//viewconfig:{//Forcefit:true//}};

Note: Autoexpandcolum can only specify the ID of a column, note, must be ID, originally we set the CM inside have no ID, now in order to use Autoexpandcolumn, To set the ID for CM's desn. So the DESN can be extended automatically when rendering, otherwise there will be an error.

var cm=new Ext.grid.ColumnModel ([{header: ' numbered ', dataindex: ' ID ', width:60}, {header: ' name ', Dataindex: ' Names ', width:180}, {ID: ' DESN ', header: ' Description ', dataindex: ' Desn ', width:200}] 3.3 Let grid support sorted by column

Sorting is easy to implement in Ext, simply by adding the sortable property when defining the column model, as shown in the following code:

var cm=new Ext.grid.ColumnModel ([{header: ' numbered ', dataindex: ' ID ', width:60,sortable:true}, {header: ' name ', Dataindex: '] Name ', width:180}, {id: ' DESN ', header: ' Description ', dataindex: ' Desn ', width:200}]

The effect figure is shown in Figure 3

Figure 3 Sorting by column effect

3.4 Displaying Time type data

Although the returned JSON is both numeric and string, in ext we can also get date-type data from the background and then give it to grid for formatting.

1, first define a set of data, the last column is date format data.

var data=[[' 1 ', ' name1 ', ' desn1 ', ' 2009-09-17t02:58:04 '], [' 2 ', ' name2 ', ' desn1 ', ' 2009-09-17t02:58:04 '], [' 3 ', ' Name3 ', ' Desn1 ', ' 2009-09-17t02:58:04 '], [' 4 ', ' name4 ', ' desn1 ', ' 2009-09-17t02:58:04 '], [' 5 ', ' name5 ', ' desn1 ', ' 2009-09-17t02 : 58:04 ']];

2. Then we add one line of configuration to the reader and set the type and DateFormat two properties in addition to the name. The code is as follows:

var store1= new Ext.data.Store ({proxy:new Ext.data.MemoryProxy (data), Reader:new Ext.data.ArrayReader ({},[{name: ' ID ') }, {name: ' name '}, {name: ' DESN '}, {name: ' Date ', type: ' Date ', DateFormat: ' Y-m-dth:i:s '}]};

3. Again, we need to add one line of configuration to CM:

var cm=new Ext.grid.ColumnModel ([{header: ' numbered ', dataindex: ' ID ', width:60,sortable:true}, {header: ' name ', Dataindex: '] Name ', width:180}, {id: ' DESN ', header: ' Description ', dataindex: ' Desn ', width:200}, {header: ' Time ', Dataindex: ' Date ', type: ' Date ', Renderer:Ext.util.Format.dateRenderer (' Y year m Month d ')}]);

4, the code details as shown below, the effect of the figure shown in Figure 4.

Figure 4 Grid with time data

3.5 Automatically display line numbers and check boxes

In fact, the line number and check box are all extensions of the renderer. Of course, the check box is much more complex to function.

1, automatic display line number: Modify the column model cm, add Rownumberer object;

2, check box: We create a checkboxselectionmodel ()

3, the detailed code is as follows, the effect chart as shown in Figure 5

var sm=new Ext.grid.CheckboxSelectionModel (); var cm=new Ext.grid.ColumnModel ([New Ext.grid.RowNumberer (), SM, {header: ' number ', Dataindex: ' id ', width:40,sortable:true }, {header: ' names ', Dataindex: ' Name ', width:180}, {id: ' DESN ', header: ' Description ', dataindex: ' Desn ', width:200}, {header: ' Time ', Dataindex: ' Date ', type: ' Date ', renderer:Ext.util.Format.dateRenderer (' Y-M-month D-day ')}];

Figure 5 Automatic line number and check box effect chart

3.6 table Pagination

The grid control has a high performance requirement, and if you display thousands of records in a grid, there is a noticeable decrease in efficiency, so you must consider paging issues.

1. Add pagination toolbar for grid: Modify the grid code on the basis of the preceding code:

var grid1=new Ext.grid.GridPanel ({renderto: "grid1", striperows:true,//zebra effect loadmask:true, store:store1, height:200, CM:CM, viewconfig:{forcefit:true}, Bbar:new Ext.pagingtoolbar ({pagesize:10, store:store1, DisplayInfo:true, Displaymsg: ' Show {0} to {1} records, altogether {2} ', Emptymsg: ' No Record '}});

2, the effect chart as shown in Figure 6:

Figure 6 Adding a page-splitting toolbar

3, if you want to truly implement pagination, you also need to get the paging data through the background script, this part no longer give four, editable table control--editorgrid

Editorgrid can be added, deleted, modified, and searched directly in the form, and then kept. There is also the dynamic modification of a cell, which we can temporarily not be empty, save will be detected, empty can not be saved, validation information will be prompted.

4.1 Make a simple Editorgrid

1, the definition column, the code is as follows:

var cm=new Ext.grid.ColumnModel ([{header: ' number ', Dataindex: ' id ', width:40,editor:new Ext.grid.GridEditor (new Ext.form.TextField ({allowblank:false})}, {header: ' names ', Dataindex: ' name ', Width:180,editor:new Ext.grid.GridEditor (New Ext.form.TextField ({allowblank:false})}, {id: ' DESN ', header: ' Description ', dataindex: ' Desn ', width:200}, {header: ' Time ', Dataindex: ' Date ', type: ' Date ', renderer:Ext.util.Format.dateRenderer (' Y-M-month D-day ')}];

2, the definition of grid, note that this is editorgridpanel.

var grid1=new Ext.grid.EditorGridPanel ({renderto: "Grid1", Store:store1, height:200, Clickstoedit:1, cm:cm});

3. By default, you need to double-click a cell to activate the editor to make changes, but you can also configure clicktoedit:1 for the grid so that you can modify it by clicking the cell activation Editor, as shown in Figure 7:

Figure 7 Modifying cells by clicking

v. Property Table--propertygrid

The property table expands from Editorgridpanel, so you can edit the contents directly to the right, note: Only the right side, even if you click the left cell, the editor will appear on the right.

The methods defined are as follows:

<mce:script type= "Text/javascript" ><!--ext.onready (function () {var grid=new Ext.grid.PropertyGrid ({title: "Property table", Autoheight:true, width:400, Renderto: ' Grid1 ', source:{"name": "Shijingming", "Creation Time": New Date (' 12/15/2009 '), "Valid": false, "version number":. 01, "description": "Estimated nothing to say"}); }); --></mce:script>

The effect figure is shown in Figure 8:

Figure 8 Property table effect Diagram vi. grouped forms--group

A grouped table is a table control that displays data in a table in a tabular form based on a column of data.

1, first define a set of data

var data=[[' 1 ', ' Male ', ' name1 ', ' DESN1 '], [' 2 ', ' Male ', ' name2 ', ' DESN1 '], [' 3 ', ' female ', ' name3 ', ' DESN1 '], [' 4 ', ' Male ', ' Name4 ', ' DESN1 '], [' 5 ', ' Female ', ' name5 ', ' DESN1 '], [' 6 ', ' Male ', ' name6 ', ' DESN1 '], [' 7 ', ' Male ', ' name7 ', ' DESN1 ']];

2, create the column information of the table

var sm=new Ext.grid.CheckboxSelectionModel (); var cm=new Ext.grid.ColumnModel ([New Ext.grid.RowNumberer (), SM, {header: ' number ', Dataindex: ' id ', width:40,sortable:true }, {header: ' Gender ', dataindex: ' Sex ', width:180}, {header: ' name ', Dataindex: ' Names ', width:200}, {header: ' description ', Dataindex: ' Desn ', width:200}]);

3. Creating data storage objects

var store1= new Ext.data.GroupingStore ({proxy:new Ext.data.MemoryProxy (data), Reader:new Ext.data.ArrayReader ({},[{ Name: ' ID '}, {name: ' Sex '}, {name: ' name '}, {name: ' DESN '}], GroupField: ' Sex ', Sortinfo:{field: ' id ', direction: ' ASC '}} ; Store1.load ();

4. Design Group Design Form

var grid1=new Ext.grid.GridPanel ({store:store1, height:300, cm:cm, View:new Ext.grid.GroupingView (), Renderto: "Grid1" });

5, the code list is as follows, the effect figure is shown in Figure 9.

 

Figure 9 Grouping tables

The table can be dragged and placed

First we look at the effect chart of Figure 10:

Figure 10 Table to drag and drop

Note that the blue strips of 104 weeks, put the mouse on the above, you can use drag and drop to change the height and width of the table, to achieve this effect is not difficult, and do not need to write a good grid big changes, as long as the original based on the addition of the following code:

var rz=new ext.resizable (' Grid1 ', {wrap:true, minheight:100, Pinned:true, handles: ' All '}); Rz.on (' resize ', grid1.syncsize,grid1);

Attention:

(1) resizable must be placed after render, otherwise there will be problems;

(2) Handles: ' All ' representatives can safely drag the table Seven, grid and right-click menu

Grid provides four events related to the right-click menu:

(1) ContextMenu: The Global Right key event;

(2) Cellcontextmenu: Right key event on cell

(3) Rowcontextmenu: Right key event on line

(4) Headercontextmenu: The right key event of the table header

The following code we implement the right key event on a line, the code list is as follows, as shown in the effect Figure 11

var contextmenu=new Ext.menu.Menu ({id: ' Thecontextmenu ', items:[{text: ' View Details ', Handler:function () {}}}); Grid1.on ("Rowcontextmenu", function (grid,rowindex,e) {e.preventdefault (); Grid1.getselectionmodel (). SelectRow ( RowIndex); Contextmenu.showat (E.getxy ()); });

Figure 11 Right-click menu

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.