Think of Ext2.0

Ext2.0的选择需要冒很大的风险,确实Ext在界面上能够有很大的提升,但是客户端机器的性能和网络方面的制约,再加上Ext本身没有很好的实现工具支持。从编码角度来说,可能需要花费更多的时间,这相对于直接的Web开发。如果项目中缺少美工,又对访问性能或并发量不是特别的要求的Web开发,可以考虑使用,譬如说安全设备的控制界面。当然项目组成员必须忍受大量javascript编码。

一般来说,Ext使用如下:

Ext 作为前台,Spring-Hibernate-Stucts作为整体的控制层,json-lib作为两者的交互。

当然后台是什么无所谓了,主要就是json字符串的拼凑

流程稍微记录了一下,后面有些不想写了,所以逻辑有些混乱。

1. Json-Lib

导入依赖库

commons-beanutils.jar

commons-collections-3.1.jar

commons-lang-2.1.jar

commons-logging.jar

ezmorph-1.0.4.jar

json-lib-2.2-jdk15.jar

测试Bean

ExpandedBlockStart.gif ContractedBlock.gif public class TestBean ... {
InBlock.gif String date1
= "111";
InBlock.gif
int date2 = 10;
ExpandedSubBlockStart.gifContractedSubBlock.gif
public String getDate1() ...{
InBlock.gif
return date1;
ExpandedSubBlockEnd.gif }

ExpandedSubBlockStart.gifContractedSubBlock.gif
public void setDate1(String date1) ...{
InBlock.gif
this.date1 = date1;
ExpandedSubBlockEnd.gif }

ExpandedSubBlockStart.gifContractedSubBlock.gif
public int getDate2() ...{
InBlock.gif
return date2;
ExpandedSubBlockEnd.gif }

ExpandedSubBlockStart.gifContractedSubBlock.gif
public void setDate2(int date2) ...{
InBlock.gif
this.date2 = date2;
ExpandedSubBlockEnd.gif }

InBlock.gif
ExpandedBlockEnd.gif}

None.gif

测试代码

None.gif JSONObject jsonBean = JSONObject.fromObject( new TestBean());
None.gif System.out.println(jsonBean);
None.gif
None.gif List list
= new ArrayList();
None.gif list.add(
new TestBean());
None.gif list.add(
new TestBean());
None.gif
None.gif JSONArray jsonList
= JSONArray.fromObject(list);
None.gif System.out.println(jsonList);
None.gif

输出:

{"date1":"111","date2":10}

[{"date1":"111","date2":10},{"date1":"111","date2":10}]

正式使用再做一下字符串处理

2. Ext使用

将整个Ext2.0的目录导入到MyEclipse中去

Docs可以全部删去,example 也可删去部分

只保留examples根目录下的内容

examples\examples.js中的开头修改成

Ext.BLANK_IMAGE_URL = 'ext-2.0/resources/images/default/s.gif';

将指向Ext网站的s.gif文件指向本地

添加 ext-fix.js 修正radioForm控件从json数据获取信息时,工作不正常的问题

ExpandedBlockStart.gif ContractedBlock.gif Ext.form.BasicForm.prototype.setValues = function (values) ... {
ExpandedSubBlockStart.gifContractedSubBlock.gif
if(values instanceof Array)...{
ExpandedSubBlockStart.gifContractedSubBlock.gif
for(var i = 0, len = values.length; i < len; i++)...{
InBlock.gif
var v = values[i];
InBlock.gif
var f = this.findField(v.id);
ExpandedSubBlockStart.gifContractedSubBlock.gif
if(f)...{
ExpandedSubBlockStart.gifContractedSubBlock.gif
if ( f.getEl().dom.type == 'radio' ) ...{
InBlock.gif
var group = this.el.dom.elements[f.getName()];
ExpandedSubBlockStart.gifContractedSubBlock.gif
for (var i=0; i < group.length; i++ ) ...{
ExpandedSubBlockStart.gifContractedSubBlock.gif
if(group[i].__ext_field) ...{
InBlock.gif group[i].__ext_field.setValue(group[i].value
== v);
ExpandedSubBlockStart.gifContractedSubBlock.gif
if(this.trackResetOnLoad)...{
InBlock.gif group[i].__ext_field.originalValue
= group[i].__ext_field.getValue();
ExpandedSubBlockEnd.gif }

ExpandedSubBlockEnd.gif }

ExpandedSubBlockEnd.gif }

ExpandedSubBlockEnd.gif }

InBlock.gif
else
ExpandedSubBlockStart.gifContractedSubBlock.gif
...{
InBlock.gif f.setValue(v.value);
ExpandedSubBlockStart.gifContractedSubBlock.gif
if(this.trackResetOnLoad)...{
InBlock.gif f.originalValue
= f.getValue();
ExpandedSubBlockEnd.gif }

ExpandedSubBlockEnd.gif }

ExpandedSubBlockEnd.gif }

ExpandedSubBlockEnd.gif }

ExpandedSubBlockStart.gifContractedSubBlock.gif }
else...{
InBlock.gif
var field, id;
ExpandedSubBlockStart.gifContractedSubBlock.gif
for(id in values)...{
ExpandedSubBlockStart.gifContractedSubBlock.gif
if(typeof values[id] != 'function' && (field = this.findField(id)))...{
ExpandedSubBlockStart.gifContractedSubBlock.gif
if( field.getEl().dom.type == 'radio' ) ...{
InBlock.gif
var group = this.el.dom.elements[field.getName()];
ExpandedSubBlockStart.gifContractedSubBlock.gif
for (var i=0; i < group.length; i++ ) ...{
ExpandedSubBlockStart.gifContractedSubBlock.gif
if(group[i].__ext_field) ...{
InBlock.gif group[i].__ext_field.setValue(group[i].value
== values[id]);
ExpandedSubBlockStart.gifContractedSubBlock.gif
if(this.trackResetOnLoad)...{
InBlock.gif group[i].__ext_field.originalValue
= group[i].__ext_field.getValue();
ExpandedSubBlockEnd.gif }

ExpandedSubBlockEnd.gif }

ExpandedSubBlockEnd.gif }

ExpandedSubBlockEnd.gif }

InBlock.gif
else
ExpandedSubBlockStart.gifContractedSubBlock.gif
...{
InBlock.gif field.setValue(values[id]);
ExpandedSubBlockStart.gifContractedSubBlock.gif
if(this.trackResetOnLoad)...{
InBlock.gif field.originalValue
= field.getValue();
ExpandedSubBlockEnd.gif }

ExpandedSubBlockEnd.gif }

ExpandedSubBlockEnd.gif }

ExpandedSubBlockEnd.gif }

ExpandedSubBlockEnd.gif }

InBlock.gif
return this;
ExpandedBlockEnd.gif }

None.gif
None.gif
None.gif
ExpandedBlockStart.gifContractedBlock.gif Ext.form.Radio.prototype.onRender
= function (ct, position) ... {
InBlock.gif Ext.form.Radio.superclass.onRender.call(
this, ct, position);
InBlock.gif
this.el.dom.__ext_field = this;
ExpandedBlockEnd.gif }

None.gif
ExpandedBlockStart.gifContractedBlock.gif Ext.form.Radio.prototype.setValue
= function (v) ... {
ExpandedSubBlockStart.gifContractedSubBlock.gif
if(v === true || v === 'true' || v == '1' || v === false || v === 'false' || v == '0') ...{
InBlock.gif
InBlock.gif
// Select all radios of this group
InBlock.gif
var radios = this.el.up('form').select('input[type=radio]');
InBlock.gif
InBlock.gif
// Uncheck all other values
ExpandedSubBlockStart.gifContractedSubBlock.gif
for(var i = 0; i < radios.elements.length; i++) ...{
InBlock.gif
if(radios.elements[i].__ext_field && radios.elements[i].__ext_field != this && radios.elements[i].name == this.el.dom.name)
ExpandedSubBlockStart.gifContractedSubBlock.gif
...{
InBlock.gif radios.elements[i].__ext_field.checked
= false;
InBlock.gif
InBlock.gif
// DOM checked is set by the browser
InBlock.gif

InBlock.gif radios.elements[i].__ext_field.fireEvent(
"check", this, this.checked);
ExpandedSubBlockEnd.gif }

ExpandedSubBlockEnd.gif }

InBlock.gif
InBlock.gif
this.checked = (v === true || v === 'true' || v == '1');
ExpandedSubBlockStart.gifContractedSubBlock.gif
if(this.el && this.el.dom) ...{
InBlock.gif
this.el.dom.checked = this.checked;
ExpandedSubBlockEnd.gif }

InBlock.gif
this.fireEvent("check", this, this.checked);
ExpandedSubBlockEnd.gif }

ExpandedBlockEnd.gif }

None.gif

在使用Ext项目中将所有的文件的编码调到UTF-8,这包括js文件和页面文件。因为Ext核心使用UTF-8编码,在表单提交和Grid时表现得比较明显。

使用Ext时,在页面文件的<head></head>中加入下面内容

None.gif < meta http-equiv ="Content-Type" content ="text/html; charset=UTF-8" >
None.gif
< title > Forms </ title >
None.gif
< link rel ="stylesheet" type ="text/css" href ="ext-2.0/resources/css/ext-all.css" />
None.gif
None.gif
<!-- GC -->
None.gif
<!-- LIBS -->
None.gif
None.gif
< script type ="text/javascript" src ="ext-2.0/adapter/ext/ext-base.js" ></ script >
None.gif
<!-- ENDLIBS -->
None.gif
None.gif
< script type ="text/javascript" src ="ext-2.0/ext-all.js" ></ script >
None.gif
< script type ="text/javascript" src ="ext-2.0/build/locale/ext-lang-zh_CN.js" ></ script >
None.gif
< script type ="text/javascript" src ="ext-2.0/ext-fix.js" ></ script >
None.gif
<!-- Common Styles for the examples -->
None.gif
< link rel ="stylesheet" type ="text/css" href ="ext-2.0/examples/examples.css" />
None.gif
< script type ="text/javascript" src ="ext-2.0/examples/examples.js" ></ script >
None.gif

这一部分可以制作页面模板

每个独立的页面 再加入自己独特的js逻辑,以dynamic.js为例

None.gif < script type ="text/javascript" src ="dynamic.js" ></ script >

如果页面需要div的话,就加入即可。

3. Form

表单制作包括提交和数据内容的填充

制作Ext Form表单, 可以使用Ext form builder来简化一些操作。

Ext Form Builder同样使用Ext开发

20080124image001.jpg

首先添加一个Form Panel

然后将所需要的Form控件拖到Form Panel上即可

选择Show/Edit JSON就可以看到生成表单所需要的javascript语句

1) 表单提交

20080124image002.jpg

制作上面的这个表单

两个Edit,一个Combo和一个Radio,代码中有些布局信息也可以使用Form Builder来制作,但是Form Builder不支持%的布局方式,可以手工改写生成界面的json来达到效果。

表单提交时,使用各个控件name值作为请求参数

这里的控件都有些布局信息,可以忽略

l Edit控件

ExpandedBlockStart.gifContractedBlock.gif... {
InBlock.gif columnWidth : .
5
,
InBlock.gif layout :
'form'
,
InBlock.gif border :
false
,
ExpandedSubBlockStart.gifContractedSubBlock.gif items : [
...
{
InBlock.gif xtype :
'textfield'
,
InBlock.gif fieldLabel :
'姓名'
,
InBlock.gif name :
'name'
,
InBlock.gif anchor :
'90%'

ExpandedSubBlockEnd.gif }
]
ExpandedBlockEnd.gif }

如果需要填写默认值的话,加入value参数即可

提交时也是使用最新的value值来提交的

l Radio控件

ExpandedBlockStart.gifContractedBlock.gif... {
InBlock.gif
InBlock.gif columnWidth : .
25
,
InBlock.gif layout :
'form'
,
InBlock.gif border :
false
,
ExpandedSubBlockStart.gifContractedSubBlock.gif items : [
...
{
InBlock.gif style :
'margin-top:5px'
,
InBlock.gif xtype :
'radio'
,
InBlock.gif fieldLabel :
'性别'
,
InBlock.gif boxLabel :
''
,
InBlock.gif name :
'sex'
,
InBlock.gif checked :
true
,
InBlock.gif inputValue :
'M'
,
InBlock.gif anchor :
'95%'

ExpandedSubBlockEnd.gif }
]
ExpandedBlockStart.gifContractedBlock.gif }
, ... {
InBlock.gif columnWidth : .
25
,
InBlock.gif layout :
'form'
,
InBlock.gif labelWidth :
0
,
InBlock.gif labelSeparator :
''
,
InBlock.gif hideLabels :
true
,
InBlock.gif border :
false
,
ExpandedSubBlockStart.gifContractedSubBlock.gif items : [
...
{
InBlock.gif style :
'margin-top:5px'
,
InBlock.gif xtype :
'radio'
,
InBlock.gif fieldLabel :
''
,
InBlock.gif boxLabel :
''
,
InBlock.gif name :
'sex'
,
InBlock.gif inputValue :
'F'
,
InBlock.gif anchor :
'95%'

ExpandedSubBlockEnd.gif }
]
ExpandedBlockEnd.gif }

None.gif

Radio使用inputValue来作为提交值,接收时也使用这个值

l Combo控件

Combo控件获取信息的Json字符串格式

ExpandedBlockStart.gifContractedBlock.gif...{totalProperty:100,root:[...{retrunValue:1, displayText:'中文1'},...{retrunValue:2, displayText:'中文2'},...{retrunValue:3, displayText:'中文3'},...{retrunValue:4, displayText:'中文4'},...{retrunValue:5, displayText:'中文5'},...{retrunValue:6, displayText:'中文6'}]}

这里使用combo.jsp来返回这些信息

None.gif<%@ page language="java" contentType="text/html; charset=UTF-8"
None.gif pageEncoding
="UTF-8"%>
None.gif
<%

None.gif
None.gifresponse.setCharacterEncoding(
"UTF-8" );
None.gif
None.gifString json
= "{totalProperty:100,root:["
;
None.gif
None.gifjson
+= "{retrunValue:1, displayText:'中文1'},"

None.gif
+"{retrunValue:2, displayText:'中文2'},"
None.gif
+"{retrunValue:3, displayText:'中文3'},"
None.gif
+"{retrunValue:4, displayText:'中文4'},"
None.gif
+"{retrunValue:5, displayText:'中文5'},"
None.gif
+"{retrunValue:6, displayText:'中文6'}]}" ;
None.gif
None.gifresponse.getWriter().write(json);
None.gif
None.gif
%>

获取的数据源

ExpandedBlockStart.gifContractedBlock.gifvar ds = new Ext.data.Store(... {
ExpandedSubBlockStart.gifContractedSubBlock.gif proxy:
new Ext.data.HttpProxy(...{url:'combo.jsp'}
),
ExpandedSubBlockStart.gifContractedSubBlock.gif reader:
new Ext.data.JsonReader(...
{
InBlock.gif totalProperty:
'totalProperty'
,
InBlock.gif root:
'root'

ExpandedSubBlockEnd.gif }
, [
ExpandedSubBlockStart.gifContractedSubBlock.gif
...{name: 'retrunValue', type: 'int'}
,
ExpandedSubBlockStart.gifContractedSubBlock.gif
...{name: 'displayText'}

InBlock.gif ])
InBlock.gif
ExpandedBlockEnd.gif }
);
None.gif

'totalProperty' json中记录的总数量

'root' json中数据开头的标示

'retrunValue' 'displayText' 为数据对象的字段名称, 其中'retrunValue'int类型

combo.jsp中获取json数据将组装成定义数据源类型

Combo使用这个数据源

ExpandedBlockStart.gif ContractedBlock.gif ... {
InBlock.gif xtype:
'combo',
InBlock.gif store: ds,
InBlock.gif valueField :
"retrunValue",
InBlock.gif displayField:
"displayText",
InBlock.gif mode:
'local',
InBlock.gif forceSelection:
true,
InBlock.gif blankText:
'请选择学历',
InBlock.gif emptyText:
'选择学历',
InBlock.gif hiddenName:
'education',
InBlock.gif editable:
false,
InBlock.gif triggerAction:
'all',
InBlock.gif allowBlank:
false,
InBlock.gif fieldLabel:
'学历',
InBlock.gif name:
'education',
InBlock.gif anchor:
'90%'
ExpandedBlockEnd.gif }
None.gif
None.gif

Combo的值为"retrunValue",显示使用"displayText"

当然最后不要忘记将ds进行load

l 保存按钮将表单信息进行提交

ExpandedBlockStart.gif ContractedBlock.gif buttons : [ ... {
InBlock.gif text :
'保存',
ExpandedSubBlockStart.gifContractedSubBlock.gif handler :
function() ...{
ExpandedSubBlockStart.gifContractedSubBlock.gif
if (simpleForm.form.isValid()) ...{
InBlock.gif
this.disabled = true;
ExpandedSubBlockStart.gifContractedSubBlock.gif simpleForm.form.doAction(
'submit', ...{
InBlock.gif url :
'test.jsp',
InBlock.gif method :
'post',
InBlock.gif params :
'',
ExpandedSubBlockStart.gifContractedSubBlock.gif success :
function(form, action) ...{
InBlock.gif Ext.Msg.alert(
'成功', action.result.data);
InBlock.gif
this.disabled = false;
InBlock.gif
//document.location.href = 'hello.html';
ExpandedSubBlockEnd.gif
}
,
ExpandedSubBlockStart.gifContractedSubBlock.gif failure :
function() ...{
InBlock.gif Ext.Msg.alert(
'失败', '抱歉');
InBlock.gif
this.disabled = false;
ExpandedSubBlockEnd.gif }

ExpandedSubBlockEnd.gif }
);
ExpandedSubBlockEnd.gif }

ExpandedSubBlockEnd.gif }

ExpandedBlockEnd.gif }
,
None.gif
None.gif

保存按钮将表单信息提交给test.jsp,我们将所有的请求参数打印出来,并返回结果信息

None.gif<%
None.gif
None.gif request.setCharacterEncoding(
"UTF-8" );
None.gif
ExpandedBlockStart.gifContractedBlock.gif
try...
{
InBlock.gif String name;
InBlock.gif java.util.Enumeration pNames
=
request.getParameterNames();
ExpandedSubBlockStart.gifContractedSubBlock.gif
while(pNames.hasMoreElements())...
{
InBlock.gif name
=
(String)pNames.nextElement();
InBlock.gif System.out.println(name
+"="+
request.getParameter(name));
ExpandedSubBlockEnd.gif }

ExpandedBlockStart.gifContractedBlock.gif }
catch(Exception e)... {
InBlock.gif System.out.print(e.toString());
ExpandedBlockEnd.gif }

None.gif
None.gif out.print(
"{success:true, data:age=567}" );
None.gif
%>

None.gif

打印处来的例子

age=dfgg

name=ddfgdfg

sex=M

education=3

返回参数也必须是json字符串,success表示操作成功,或失败

action.result.data会取=后的值

20080124image003.jpg

2) 表单接收信息

只需要返回和表单相符的JSON字符串

{success:true,data:{"name":'你好',"age":'131',education:3,sex:'F'}}

formget.jsp文件内容

None.gif <% @ page language = " java " contentType = " text/html; charset=UTF-8 "
None.gif pageEncoding
= " UTF-8 " %>
None.gif
<%
None.gif
None.gifout.print(
" {success:true,data:{"name":'你好',"age":'131',education:3,sex:'F'}} " );
None.gif
None.gif
%>
None.gif
None.gif

取消按钮从formget.jsp中获取这个字符串信息,来填充表单

None.gif buttons : [
None.gif...
ExpandedBlockStart.gifContractedBlock.gif
... {
InBlock.gif text :
'取消',
ExpandedSubBlockStart.gifContractedSubBlock.gif handler :
function() ...{
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif simpleForm.form.load(
...{
InBlock.gif url :
'formget.jsp',
InBlock.gif method :
'get',
InBlock.gif params :
''
InBlock.gif
ExpandedSubBlockEnd.gif }
);
InBlock.gif
ExpandedSubBlockEnd.gif }

InBlock.gif

当然也可以在开始时,就调用来导入数据

4. Grid

20080124image004.jpg

Grid所需要的json字符串

{totalProperty:100,root:[{id:1,name:'二月DD1',descn:'descn1'},{id:2,name:'二月DD2',descn:'descn2'},{id:3,name:'二月DD3',descn:'descn3'},{id:4,name:'二月DD4',descn:'descn4'},{id:5,name:'二月DD5',descn:'descn5'},{id:6,name:'二月DD6',descn:'descn6'},{id:7,name:'二月DD7',descn:'descn7'},{id:8,name:'二月DD8',descn:'descn8'},{id:9,name:'二月DD9',descn:'descn9'},{id:10,name:'二月DD10',descn:'descn10'}]}

Grid.jsp 根据起始值和限制数量决定返回json字符串

None.gif <% @ page language = " java " contentType = " text/html; charset=UTF-8 "
None.gif pageEncoding
= " UTF-8 " %>
None.gif
<%
None.gif
// response.setCharacterEncoding("UTF-8");
None.gif
String start = request.getParameter( " start " );
None.gifString limit
= request.getParameter( " limit " );
ExpandedBlockStart.gifContractedBlock.gif
try ... {
InBlock.gif
int index = Integer.parseInt(start);
InBlock.gif
int pageSize = Integer.parseInt(limit);
InBlock.gif
InBlock.gif
//String json = "{totalProperty:100,root:[";
InBlock.gif
String json = "{totalProperty:100,root:[";
ExpandedSubBlockStart.gifContractedSubBlock.gif
for (int i = index; i < pageSize + index; i++) ...{
InBlock.gif json
+= "{id:" + i + ",name:'二月DD" + i + "',descn:'descn" + i + "'}";
ExpandedSubBlockStart.gifContractedSubBlock.gif
if (i != pageSize + index - 1) ...{
InBlock.gif json
+= ",";
ExpandedSubBlockEnd.gif }

ExpandedSubBlockEnd.gif }

InBlock.gif json
+= "]}";
InBlock.gif response.getWriter().write(json);
InBlock.gif
//out.print(json);
ExpandedBlockStart.gifContractedBlock.gif
}
catch (Exception ex) ... {
ExpandedBlockEnd.gif}

None.gif
%>
None.gif

获取数据时,如此访问grid.jsp文件

grid.jsp?start=1&limit=10

Grid使用

Grid中字段定制

None.gif var cm = new Ext.grid.ColumnModel([
ExpandedBlockStart.gifContractedBlock.gif
... {header:'描述',dataIndex:'id'} ,
ExpandedBlockStart.gifContractedBlock.gif
... {header:'姓名',width:100, sortable:true,dataIndex:'name'} ,
ExpandedBlockStart.gifContractedBlock.gif
... {header:'描述',dataIndex:'descn'}
None.gif ]);
None.gif

Header 显示名称

dataIndex ds查找字段

width 字段宽度

sortable 是否允许排序

Grid中使用数据源的定义,从grid.jsp中获取数据


ExpandedBlockStart.gif ContractedBlock.gif var ds = new Ext.data.Store( ... {
ExpandedSubBlockStart.gifContractedSubBlock.gif proxy:
new Ext.data.HttpProxy(...{url:'grid.jsp'}),
ExpandedSubBlockStart.gifContractedSubBlock.gif reader:
new Ext.data.JsonReader(...{
InBlock.gif totalProperty:
'totalProperty',
InBlock.gif root:
'root'
ExpandedSubBlockEnd.gif }
, [
ExpandedSubBlockStart.gifContractedSubBlock.gif
...{name: 'id'},
ExpandedSubBlockStart.gifContractedSubBlock.gif
...{name: 'name'},
ExpandedSubBlockStart.gifContractedSubBlock.gif
...{name: 'descn'}
InBlock.gif ])
InBlock.gif
ExpandedBlockEnd.gif }
);
None.gif

定义GridPanel创建Grid

ExpandedBlockStart.gif ContractedBlock.gif var grid = new Ext.grid.GridPanel( ... {
InBlock.gif el:
'grid',
InBlock.gif width:
600,
InBlock.gif ds: ds,
InBlock.gif cm: cm,
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif bbar:
new Ext.PagingToolbar(...{
InBlock.gif pageSize:
10,
InBlock.gif store: ds,
InBlock.gif displayInfo:
true,
InBlock.gif displayMsg:
'显示第 {0} 条到 {1} 条记录,一共 {2} 条',
InBlock.gif emptyMsg:
"你好"
ExpandedSubBlockEnd.gif }
)
InBlock.gif
ExpandedBlockEnd.gif }
);
None.gif
None.gif grid.render();
None.gif

elgrid.htmlidgriddiv

ds 数据源

cm grid显示列定义

bbar bottom toolbal下面的工具栏

这里使用分页控件

最后ds导入时候,使用参数进行过滤

ds.load({params:{start:0,limit:10}});

扩展一下,将上面的Form放入到grid中来

Grid上添加一个工具栏,通过单击工具栏中Add Something按钮,弹出上面的Form信息

20080124image005.jpg

修改如下:

ExpandedBlockStart.gif ContractedBlock.gif var grid = new Ext.grid.GridPanel( ... {
InBlock.gif el:
'grid',
InBlock.gif width:
600,
InBlock.gif ds: ds,
InBlock.gif cm: cm,
ExpandedSubBlockStart.gifContractedSubBlock.gif tbar:[
...{
InBlock.gif text:
'Add Something',
InBlock.gif tooltip:
'Add a new row',
InBlock.gif iconCls:
'add',
InBlock.gif handler : onItemClick
ExpandedSubBlockStart.gifContractedSubBlock.gif }
, '-', ...{
InBlock.gif text:
'Options',
InBlock.gif tooltip:
'Blah blah blah blaht',
InBlock.gif iconCls:
'option'
ExpandedSubBlockStart.gifContractedSubBlock.gif }
,'-',...{
InBlock.gif text:
'Remove Something',
InBlock.gif tooltip:
'Remove the selected item',
InBlock.gif iconCls:
'remove'
ExpandedSubBlockEnd.gif }
],
ExpandedSubBlockStart.gifContractedSubBlock.gif bbar:
new Ext.PagingToolbar(...{
InBlock.gif pageSize:
10,
InBlock.gif store: ds,
InBlock.gif displayInfo:
true,
InBlock.gif displayMsg:
'显示第 {0} 条到 {1} 条记录,一共 {2} 条',
InBlock.gif emptyMsg:
"你好"
ExpandedSubBlockEnd.gif }
)
InBlock.gif
ExpandedBlockEnd.gif }
);
None.gif

在GridPanel添加在Grid上的Toolbar,Toolbar上添加三个按钮,并为Add Something添加单击事件onItemClick。

单击事件

None.gif var win;
ExpandedBlockStart.gifContractedBlock.gif
function onItemClick(item) ... {
InBlock.gif
//alert(item.text);
InBlock.gif

ExpandedSubBlockStart.gifContractedSubBlock.gif
if (!win) ...{
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
var ds = new Ext.data.Store(...{
ExpandedSubBlockStart.gifContractedSubBlock.gif proxy:
new Ext.data.HttpProxy(...{url:'combo.jsp'}),
ExpandedSubBlockStart.gifContractedSubBlock.gif reader:
new Ext.data.JsonReader(...{
InBlock.gif totalProperty:
'totalProperty',
InBlock.gif root:
'root'
ExpandedSubBlockEnd.gif }
, [
ExpandedSubBlockStart.gifContractedSubBlock.gif
...{name: 'retrunValue', type: 'int'},
ExpandedSubBlockStart.gifContractedSubBlock.gif
...{name: 'displayText'}
InBlock.gif ])
ExpandedSubBlockEnd.gif }
);
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
var simpleForm = new Ext.FormPanel( ...{
InBlock.gif el :
'hello-tabs',
InBlock.gif labelAlign :
'left',
InBlock.gif title :
'你好',
InBlock.gif buttonAlign :
'right',
InBlock.gif bodyStyle :
'padding:5px',
InBlock.gif width :
600,
InBlock.gif frame :
true,
InBlock.gif labelWidth :
80,
ExpandedSubBlockStart.gifContractedSubBlock.gif items : [
...{
InBlock.gif xtype :
"textfield",
InBlock.gif fieldLabel :
"Text",
InBlock.gif name :
"textvalue"
ExpandedSubBlockEnd.gif }
],
ExpandedSubBlockStart.gifContractedSubBlock.gif items : [
...{
InBlock.gif layout :
'column',
InBlock.gif border :
false,
InBlock.gif labelSeparator :
':',
ExpandedSubBlockStart.gifContractedSubBlock.gif items : [
...{
InBlock.gif columnWidth : .
5,
InBlock.gif layout :
'form',
InBlock.gif border :
false,
ExpandedSubBlockStart.gifContractedSubBlock.gif items : [
...{
InBlock.gif xtype :
'textfield',
InBlock.gif fieldLabel :
'姓名',
InBlock.gif name :
'name',
InBlock.gif anchor :
'90%'
ExpandedSubBlockEnd.gif }
]
ExpandedSubBlockStart.gifContractedSubBlock.gif }
, ...{
InBlock.gif columnWidth : .
5,
InBlock.gif layout :
'form',
InBlock.gif border :
false,
ExpandedSubBlockStart.gifContractedSubBlock.gif items : [
...{
InBlock.gif xtype :
'textfield',
InBlock.gif fieldLabel :
'年龄',
InBlock.gif name :
'age',
InBlock.gif anchor :
'90%'
ExpandedSubBlockEnd.gif }
]
ExpandedSubBlockStart.gifContractedSubBlock.gif }
, ...{
InBlock.gif columnWidth : .
5,
InBlock.gif layout :
'form',
InBlock.gif border :
false,
ExpandedSubBlockStart.gifContractedSubBlock.gif items : [
...{
InBlock.gif xtype :
'combo',
InBlock.gif store : ds,
InBlock.gif valueField :
"retrunValue",
InBlock.gif displayField :
"displayText",
InBlock.gif mode :
'local',
InBlock.gif forceSelection :
true,
InBlock.gif blankText :
'请选择学历',
InBlock.gif emptyText :
'选择学历',
InBlock.gif hiddenName :
'education',
InBlock.gif editable :
false,
InBlock.gif triggerAction :
'all',
InBlock.gif allowBlank :
false,
InBlock.gif fieldLabel :
'学历',
InBlock.gif name :
'education',
InBlock.gif anchor :
'90%'
ExpandedSubBlockEnd.gif }
]
ExpandedSubBlockEnd.gif }
,
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
...{
InBlock.gif
InBlock.gif columnWidth : .
25,
InBlock.gif layout :
'form',
InBlock.gif border :
false,
ExpandedSubBlockStart.gifContractedSubBlock.gif items : [
...{
InBlock.gif style :
'margin-top:5px',
InBlock.gif xtype :
'radio',
InBlock.gif fieldLabel :
'性别',
InBlock.gif boxLabel :
'',
InBlock.gif name :
'sex',
InBlock.gif checked :
true,
InBlock.gif inputValue :
'M',
InBlock.gif anchor :
'95%'
ExpandedSubBlockEnd.gif }
]
ExpandedSubBlockStart.gifContractedSubBlock.gif }
, ...{
InBlock.gif columnWidth : .
25,
InBlock.gif layout :
'form',
InBlock.gif labelWidth :
0,
InBlock.gif labelSeparator :
'',
InBlock.gif hideLabels :
true,
InBlock.gif border :
false,
ExpandedSubBlockStart.gifContractedSubBlock.gif items : [
...{
InBlock.gif style :
'margin-top:5px',
InBlock.gif xtype :
'radio',
InBlock.gif fieldLabel :
'',
InBlock.gif boxLabel :
'',
InBlock.gif name :
'sex',
InBlock.gif inputValue :
'F',
InBlock.gif anchor :
'95%'
ExpandedSubBlockEnd.gif }
]
ExpandedSubBlockEnd.gif }

InBlock.gif
InBlock.gif ]
ExpandedSubBlockEnd.gif }
]
ExpandedSubBlockEnd.gif }
);
InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif win
= new Ext.Window( ...{
InBlock.gif el :
'hello-win',
InBlock.gif layout :
'fit',
InBlock.gif width :
500,
InBlock.gif height :
300,
InBlock.gif closeAction :
'hide',
InBlock.gif plain :
true,
InBlock.gif
InBlock.gif items :
InBlock.gif simpleForm,
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif buttons : [
...{
InBlock.gif text :
'保存',
ExpandedSubBlockStart.gifContractedSubBlock.gif handler :
function() ...{
ExpandedSubBlockStart.gifContractedSubBlock.gif
if (simpleForm.form.isValid()) ...{
InBlock.gif
this.disabled = true;
ExpandedSubBlockStart.gifContractedSubBlock.gif simpleForm.form.doAction(
'submit', ...{
InBlock.gif url :
'test.jsp',
InBlock.gif method :
'post',
InBlock.gif params :
'',
ExpandedSubBlockStart.gifContractedSubBlock.gif success :
function(form, action) ...{
InBlock.gif Ext.Msg.alert(
'成功', action.result.data);
InBlock.gif
this.disabled = false;
InBlock.gif
//document.location.href = 'hello.html';
ExpandedSubBlockEnd.gif
}
,
ExpandedSubBlockStart.gifContractedSubBlock.gif failure :
function() ...{
InBlock.gif Ext.Msg.alert(
'失败', '抱歉');
InBlock.gif
this.disabled = false;
ExpandedSubBlockEnd.gif }

ExpandedSubBlockEnd.gif }
);
ExpandedSubBlockEnd.gif }

ExpandedSubBlockEnd.gif }

ExpandedSubBlockStart.gifContractedSubBlock.gif }
, ...{
InBlock.gif text :
'取消',
ExpandedSubBlockStart.gifContractedSubBlock.gif handler :
function() ...{
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif simpleForm.form.load(
...{
InBlock.gif url :
'formget.jsp',
InBlock.gif method :
'get',
InBlock.gif params :
''
InBlock.gif
ExpandedSubBlockEnd.gif }
);
InBlock.gif
ExpandedSubBlockEnd.gif }

ExpandedSubBlockEnd.gif }
]
InBlock.gif
ExpandedSubBlockEnd.gif }
);
InBlock.gif
InBlock.gif
InBlock.gif ds.load();
ExpandedSubBlockEnd.gif }

InBlock.gif win.show(
this);
ExpandedBlockEnd.gif }

None.gif

唯一需要注意的是保存和取消按钮需要加入Ext.Window做控制,并且要注意FormPanelExt.Windows所要渲染的div块,并且这些块必须在grid.html中进行定义

None.gif < div id = " hello-win " class = " x-hidden " >
None.gif
< div class = " x-window-header " > Hello Dialog </ div >
None.gif
< div id = " hello-tabs " >
None.gif
<!-- Auto create tab 1 -->
None.gif
< div class = " x-tab " title = " Hello World 1 " >
None.gif
< p > Hello... </ p >
None.gif
</ div >
None.gif
<!-- Auto create tab 2 -->
None.gif
< div class = " x-tab " title = " Hello World 2 " >
None.gif
< p > ... World !</ p >
None.gif
</ div >
None.gif
</ div >
None.gif
</ div >
None.gif

5. Tree

20080124image006.jpg

直接使用Ext例子

Tree所需要的json字符串

[{id:300,text:'01',cls:'task-folder',children:[{id:'2',text:'01-01',leaf:true, cls:'task'},{id:'3',text:'01-02',children:[{id:'4',text:'01-02-01',leaf:true},{id:'5',text:'01-02-02',leaf:true}]},{id:'6',text:'01-03',leaf:true}]},{id:'7',text:'02',leaf:true}]

text"-->显示的文本
"id"-->id值 ,单击事件时可以使用

"leaf"-->Boolean值,如果"叶子"是真的话,则不能包含子节点Children nodes

"cls"-->选用的样式,通常在这里选定图标

"href"-->指定的url,还有一个"hrefTarget"的属性

children -〉表示子节点信息

Record.css中自定了两个定义的csstasktask-folder

.task .x-tree-node-icon {

background-image:url(icons/cog.png) ;

}

.task-folder .x-tree-node-icon{

background-image:url(icons/folder_go.png) !important;

}

Json字符串中就使用了这个值

Tree使用如下,相对比较简单

None.gif var Tree = Ext.tree;
None.gif
ExpandedBlockStart.gifContractedBlock.gif
var tree = new Tree.TreePanel( ... {
InBlock.gif el:
'tree-div',
InBlock.gif autoScroll:
true,
InBlock.gif animate:
true,
InBlock.gif enableDD:
true,
InBlock.gif containerScroll:
true,
ExpandedSubBlockStart.gifContractedSubBlock.gif loader:
new Tree.TreeLoader(...{
InBlock.gif dataUrl:
'record.jsp'
ExpandedSubBlockEnd.gif }
)
ExpandedBlockEnd.gif }
);
None.gif
None.gif
// set the root node
ExpandedBlockStart.gifContractedBlock.gif
var root = new Tree.AsyncTreeNode( ... {
InBlock.gif text:
'Ext JS',
InBlock.gif draggable:
false,
InBlock.gif id:
'source'
ExpandedBlockEnd.gif }
);
None.gif tree.setRootNode(root);
None.gif
None.gif
// render the tree
None.gif
tree.render();
None.gif root.expand();
None.gif


6. Layout

20080124image007.jpg

左边为树型控件,右边为TabPanel,显示的页面

当左边树型控件被点击后,右边的所有的Tab进行变化,访问与树型列表id相对应的页面内容

l 布局使用

布局一般使用Viewport

var viewport = new Ext.Viewport({

只要注意regionel即可

region:'north',

el:'north-div',

elcontentEl的区别

el是第一层div contentEl一般指向更内部的div

l Tab使用

普通的Tab访问未有无法显示js的问题

{

title: 'Ajax Tab 1',

autoLoad:{url:'grid.html',scripts:true}

}

所以就使用了Ext论坛中提供的一个Iframe的扩展 Ext.ux.ManagedIFrame

使用时将miframe.js文件导入即可

ExpandedBlockStart.gif ContractedBlock.gif var tabs2 = new Ext.TabPanel( ... {
InBlock.gif
// renderTo: document.body,
InBlock.gif
region : 'center',
InBlock.gif el :
'center-center', // 大的
InBlock.gif
//contentEl : 'center-center', // 小的
InBlock.gif
activeTab : 0,
InBlock.gif width :
600,
InBlock.gif height :
250,
InBlock.gif plain :
true,
ExpandedSubBlockStart.gifContractedSubBlock.gif defaults :
...{
InBlock.gif autoScroll :
true
ExpandedSubBlockEnd.gif }
,
InBlock.gif items : [
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
...{
InBlock.gif xtype :
"panel",
InBlock.gif title :
"Personal Assistant",
ExpandedSubBlockStart.gifContractedSubBlock.gif body :
new Ext.ux.ManagedIFrame( ...{
ExpandedSubBlockStart.gifContractedSubBlock.gif autoCreate :
...{
InBlock.gif id:
'person', // 设置访问的名称
InBlock.gif
src : 'dynamic.html',
InBlock.gif frameBorder :
0,
InBlock.gif cls :
'x-panel-body',
InBlock.gif width :
'100%',
InBlock.gif height :
'100%'
ExpandedSubBlockEnd.gif }

ExpandedSubBlockEnd.gif }
)
ExpandedSubBlockEnd.gif }
,
ExpandedSubBlockStart.gifContractedSubBlock.gif
...{
InBlock.gif xtype :
"panel",
InBlock.gif title :
"Personal",
ExpandedSubBlockStart.gifContractedSubBlock.gif body :
new Ext.ux.ManagedIFrame( ...{
ExpandedSubBlockStart.gifContractedSubBlock.gif autoCreate :
...{
InBlock.gif id:
'person2', // 设置访问的名称
InBlock.gif
src : 'grid.html',
InBlock.gif frameBorder :
0,
InBlock.gif cls :
'x-panel-body',
InBlock.gif width :
'100%',
InBlock.gif height :
'100%'
ExpandedSubBlockEnd.gif }

ExpandedSubBlockEnd.gif }
)
ExpandedSubBlockEnd.gif }

InBlock.gif ]
ExpandedBlockEnd.gif }
);
None.gif

l 树型控件

要给树型控件添加单击事件

ExpandedBlockStart.gif ContractedBlock.gif tree.on( ' click ' , function (node) ... {
InBlock.gif
// do something
InBlock.gif
alert(node.id + ' was activated.');
InBlock.gif Ext.get(
'person').dom.src = 'grid.html?selectedid='+node.id;
ExpandedBlockEnd.gif }
);
None.gif

单击后,通过查找ManagedIFrame形成的Tab页的id,并且将其属性src改变来达到Tab页内容改变的效果

None.gif Ext.get( ' person ' ).dom.src = ' grid.html?selectedid= ' + node.id;

参考:

Ext2_0 form使用实例 - 天晓得的专栏 - CSDNBlog

Ext 2_0布局实例

[2_0][SOLVED] Best practices for getting - saving form data - Ext JS Forums

[EXT Develop Log]--comboBoxradioFix it! - kkbear - JavaEye技术网站

ext学习-tree组件-在线阅读-新书城

对《Ext2_0 form使用实例》的一点补充 - 天晓得的专栏 - CSDNBlog

Ext 2_0 combobox 做的省份和城市联动选择框 - 天晓得的专栏 - CSDNBlog

关于extstruts的交互 - Allen_CD_China - JavaEye技术网站

Java 类产生json(json-lib) - windfree - BlogJava

学习EXTXX

流氓临远, 没人性土豆's ext tutorial

And Others, I can't remember

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值