`
dato0123
  • 浏览: 915469 次
文章分类
社区版块
存档分类
最新评论

ExtJS 2.0入门指南

 
阅读更多
<meta content="Word.Document" name="ProgId"> <meta content="Microsoft Word 11" name="Generator"> <meta content="Microsoft Word 11" name="Originator"> <link href="file:///C:%5CDOCUME%7E1%5Cphinecos%5CLOCALS%7E1%5CTemp%5Cmsohtml1%5C01%5Cclip_filelist.xml" rel="File-List"> <link href="file:///C:%5CDOCUME%7E1%5Cphinecos%5CLOCALS%7E1%5CTemp%5Cmsohtml1%5C01%5Cclip_editdata.mso" rel="Edit-Time-Data"> <!--[if !mso]> <style> v/:* {behavior:url(#default#VML);} o/:* {behavior:url(#default#VML);} w/:* {behavior:url(#default#VML);} .shape {behavior:url(#default#VML);} </style> <![endif]--><!--[if gte mso 9]><xml> <w:WordDocument> <w:View>Normal</w:View> <w:Zoom>0</w:Zoom> <w:PunctuationKerning/> <w:DrawingGridVerticalSpacing>7.8 磅</w:DrawingGridVerticalSpacing> <w:DisplayHorizontalDrawingGridEvery>0</w:DisplayHorizontalDrawingGridEvery> <w:DisplayVerticalDrawingGridEvery>2</w:DisplayVerticalDrawingGridEvery> <w:ValidateAgainstSchemas/> <w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid> <w:IgnoreMixedContent>false</w:IgnoreMixedContent> <w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText> <w:Compatibility> <w:SpaceForUL/> <w:BalanceSingleByteDoubleByteWidth/> <w:DoNotLeaveBackslashAlone/> <w:ULTrailSpace/> <w:DoNotExpandShiftReturn/> <w:AdjustLineHeightInTable/> <w:BreakWrappedTables/> <w:SnapToGridInCell/> <w:WrapTextWithPunct/> <w:UseAsianBreakRules/> <w:DontGrowAutofit/> <w:UseFELayout/> </w:Compatibility> <w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel> </w:WordDocument> </xml><![endif]--><!--[if gte mso 9]><xml> <w:LatentStyles deflockedstate="false" latentstylecount="156"> </w:LatentStyles> </xml><![endif]--><style> <!-- /* Font Definitions */ &#64;font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-alt:SimSun; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} &#64;font-face {font-family:新宋体; panose-1:2 1 6 9 3 1 1 1 1 1; mso-font-charset:134; mso-generic-font-family:modern; mso-font-pitch:fixed; mso-font-signature:3 135135232 16 0 262145 0;} &#64;font-face {font-family:Verdana; panose-1:2 11 6 4 3 5 4 4 2 4; mso-font-charset:0; mso-generic-font-family:swiss; mso-font-pitch:variable; mso-font-signature:536871559 0 0 0 415 0;} &#64;font-face {font-family:"/&#64;宋体"; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} &#64;font-face {font-family:"/&#64;新宋体"; panose-1:2 1 6 9 3 1 1 1 1 1; mso-font-charset:134; mso-generic-font-family:modern; mso-font-pitch:fixed; mso-font-signature:3 135135232 16 0 262145 0;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; mso-pagination:none; font-size:10.5pt; mso-bidi-font-size:12.0pt; font-family:"Times New Roman"; mso-fareast-font-family:宋体; mso-font-kerning:1.0pt;} /* Page Definitions */ &#64;page {mso-page-border-surround-header:no; mso-page-border-surround-footer:no;} &#64;page Section1 {size:612.0pt 792.0pt; margin:72.0pt 90.0pt 72.0pt 90.0pt; mso-header-margin:36.0pt; mso-footer-margin:36.0pt; mso-paper-source:0;} div.Section1 {page:Section1;} --> </style> <!--[if gte mso 10]> <style> /* Style Definitions */ table.MsoNormalTable {mso-style-name:普通表格; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-parent:""; mso-padding-alt:0cm 5.4pt 0cm 5.4pt; mso-para-margin:0cm; mso-para-margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:10.0pt; font-family:"Times New Roman"; mso-ansi-language:#0400; mso-fareast-language:#0400; mso-bidi-language:#0400;} </style> <![endif]-->

<!--[if gte vml 1]><v:shapetype id="_x0000_t75" coordsize="21600,21600" o:spt="75" o:preferrelative="t" path="m&#64;4&#64;5l&#64;4&#64;11&#64;9&#64;11&#64;9&#64;5xe" filled="f" stroked="f"> <v:stroke joinstyle="miter" /> <v:formulas> <v:f eqn="if lineDrawn pixelLineWidth 0" /> <v:f eqn="sum &#64;0 1 0" /> <v:f eqn="sum 0 0 &#64;1" /> <v:f eqn="prod &#64;2 1 2" /> <v:f eqn="prod &#64;3 21600 pixelWidth" /> <v:f eqn="prod &#64;3 21600 pixelHeight" /> <v:f eqn="sum &#64;0 0 1" /> <v:f eqn="prod &#64;6 1 2" /> <v:f eqn="prod &#64;7 21600 pixelWidth" /> <v:f eqn="sum &#64;8 21600 0" /> <v:f eqn="prod &#64;7 21600 pixelHeight" /> <v:f eqn="sum &#64;10 21600 0" /> </v:formulas> <v:path o:extrusionok="f" gradientshapeok="t" o:connecttype="rect" /> <o:lock v:ext="edit" aspectratio="t" /> </v:shapetype><v:shape id="_x0000_i1025" type="#_x0000_t75" alt="" href="http://extjs.com/learn/w/index.php?title=Tutorial:Introduction_to_Ext_2.0_%28Chinese%29&amp;action=edit&amp;section=2" title="&quot;Edit section: 开始!&quot;" style='width:12pt;height:12pt' o:button="t"> <v:imagedata src="file:///C:/DOCUME~1/phinecos/LOCALS~1/Temp/msohtml1/01/clip_image001.png" o:href="http://extjs.com/learn/w/skins/extjs/page_edit.png" /> </v:shape><![endif]--><!--[if !vml]--><!--[endif]-->开始!

我们将讲讲怎么使用ExtJS,来完成一些JavaScript常见的任务。如果你想自己试试,就应该先下IntroToExt2.zip,用来构建已下面的Ext代码。

Zip包里有三个文件:ExtStart.html,ExtStart.jsExtStart.css。解包这三个文件到Ext的安装目录中(例如,Ext是在“C:/code/Ext/v2.0”中,那应该在"v2.0"里面新建目录“tutorial”。双击ExtStart.htm,接着你的浏览器打开启动页面,应该会有一条消息告诉你配置已完毕。如果出现了Javascript错误,请按照页面上的指引操作。

现在在你常用的IDE中或文本编辑器中,打开ExtStart.js看看。

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->Ext.onReady(function(){alert("Congratulations!YouhaveExtconfiguredcorrectly!");});

<meta content="Word.Document" name="ProgId"><meta content="Microsoft Word 11" name="Generator"><meta content="Microsoft Word 11" name="Originator"><link href="file:///C:%5CDOCUME%7E1%5Cphinecos%5CLOCALS%7E1%5CTemp%5Cmsohtml1%5C01%5Cclip_filelist.xml" rel="File-List"><!--[if gte mso 9]><xml> <w:WordDocument> <w:View>Normal</w:View> <w:Zoom>0</w:Zoom> <w:PunctuationKerning/> <w:DrawingGridVerticalSpacing>7.8 磅</w:DrawingGridVerticalSpacing> <w:DisplayHorizontalDrawingGridEvery>0</w:DisplayHorizontalDrawingGridEvery> <w:DisplayVerticalDrawingGridEvery>2</w:DisplayVerticalDrawingGridEvery> <w:ValidateAgainstSchemas/> <w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid> <w:IgnoreMixedContent>false</w:IgnoreMixedContent> <w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText> <w:Compatibility> <w:SpaceForUL/> <w:BalanceSingleByteDoubleByteWidth/> <w:DoNotLeaveBackslashAlone/> <w:ULTrailSpace/> <w:DoNotExpandShiftReturn/> <w:AdjustLineHeightInTable/> <w:BreakWrappedTables/> <w:SnapToGridInCell/> <w:WrapTextWithPunct/> <w:UseAsianBreakRules/> <w:DontGrowAutofit/> <w:UseFELayout/> </w:Compatibility> <w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel> </w:WordDocument> </xml><![endif]--><!--[if gte mso 9]><xml> <w:LatentStyles deflockedstate="false" latentstylecount="156"> </w:LatentStyles> </xml><![endif]--><style> <!-- /* Font Definitions */ &#64;font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-alt:SimSun; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} &#64;font-face {font-family:Verdana; panose-1:2 11 6 4 3 5 4 4 2 4; mso-font-charset:0; mso-generic-font-family:swiss; mso-font-pitch:variable; mso-font-signature:536871559 0 0 0 415 0;} &#64;font-face {font-family:"/&#64;宋体"; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; mso-pagination:none; font-size:10.5pt; mso-bidi-font-size:12.0pt; font-family:"Times New Roman"; mso-fareast-font-family:宋体; mso-font-kerning:1.0pt;} /* Page Definitions */ &#64;page {mso-page-border-surround-header:no; mso-page-border-surround-footer:no;} &#64;page Section1 {size:612.0pt 792.0pt; margin:72.0pt 90.0pt 72.0pt 90.0pt; mso-header-margin:36.0pt; mso-footer-margin:36.0pt; mso-paper-source:0;} div.Section1 {page:Section1;} --> </style><!--[if gte mso 10]> <style> /* Style Definitions */ table.MsoNormalTable {mso-style-name:普通表格; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-parent:""; mso-padding-alt:0cm 5.4pt 0cm 5.4pt; mso-para-margin:0cm; mso-para-margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:10.0pt; font-family:"Times New Roman"; mso-ansi-language:#0400; mso-fareast-language:#0400; mso-bidi-language:#0400;} </style> <![endif]-->

Ext.onReady可能是你接触的第一个也可能是在每个页面都要使用的方法。这个方法会在DOM加载全部完毕后,保证页面内的所有元素能被Script引用(reference)之后调用。你可删除alert()那行,加入一些实际用途的代码试试。

<meta content="Word.Document" name="ProgId"><meta content="Microsoft Word 11" name="Generator"><meta content="Microsoft Word 11" name="Originator"><link href="file:///C:%5CDOCUME%7E1%5Cphinecos%5CLOCALS%7E1%5CTemp%5Cmsohtml1%5C01%5Cclip_filelist.xml" rel="File-List"><!--[if gte mso 9]><xml> Normal 0 7.8 磅 0 2 false false false MicrosoftInternetExplorer4 </xml><![endif]--><!--[if gte mso 9]><![endif]--><style> <!-- /* Font Definitions */ &#64;font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1;} &#64;font-face {font-family:Verdana; panose-1:2 11 6 4 3 5 4 4 2 4;} &#64;font-face {font-family:""&#64;宋体"; panose-1:2 1 6 0 3 1 1 1 1 1;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; font-size:10.5pt; font-family:"Times New Roman";} /* Page Definitions */ &#64;page {} &#64;page Section1 {size:612.0pt 792.0pt; margin:72.0pt 90.0pt 72.0pt 90.0pt;} div.Section1 {page:Section1;} --> </style><!--[if gte mso 10]> <style> /* Style Definitions */ table.MsoNormalTable { mso-style-parent:""; font-size:10.0pt; font-family:"Times New Roman";} </style> <![endif]-->

ElementExt的核心

<meta content="Word.Document" name="ProgId"><meta content="Microsoft Word 11" name="Generator"><meta content="Microsoft Word 11" name="Originator"><link href="file:///C:%5CDOCUME%7E1%5Cphinecos%5CLOCALS%7E1%5CTemp%5Cmsohtml1%5C01%5Cclip_filelist.xml" rel="File-List"><!--[if gte mso 9]><xml> Normal 0 7.8 磅 0 2 false false false MicrosoftInternetExplorer4 </xml><![endif]--><!--[if gte mso 9]><![endif]--><style> <!-- /* Font Definitions */ &#64;font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1;} &#64;font-face {font-family:Verdana; panose-1:2 11 6 4 3 5 4 4 2 4;} &#64;font-face {font-family:""&#64;宋体"; panose-1:2 1 6 0 3 1 1 1 1 1;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; font-size:10.5pt; font-family:"Times New Roman";} /* Page Definitions */ &#64;page {} &#64;page Section1 {size:612.0pt 792.0pt; margin:72.0pt 90.0pt 72.0pt 90.0pt;} div.Section1 {page:Section1;} --> </style><!--[if gte mso 10]> <style> /* Style Definitions */ table.MsoNormalTable { mso-style-parent:""; font-size:10.0pt; font-family:"Times New Roman";} </style> <![endif]-->

大多数的JavaScript操作都需要先获取页面上的某个元素的引用(reference,好让你来做些实质性的事情。传统的JavaScript做法,是通过ID获取Dom节点的:

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->varmyDiv=document.getElementById('myDiv');

<meta content="Word.Document" name="ProgId"> <meta content="Microsoft Word 11" name="Generator"> <meta content="Microsoft Word 11" name="Originator"> <link href="file:///C:%5CDOCUME%7E1%5Cphinecos%5CLOCALS%7E1%5CTemp%5Cmsohtml1%5C01%5Cclip_filelist.xml" rel="File-List"> <!--[if gte mso 9]><xml> Normal 0 7.8 磅 0 2 false false false MicrosoftInternetExplorer4 </xml><![endif]--><!--[if gte mso 9]><![endif]--><style> <!-- /* Font Definitions */ &#64;font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1;} &#64;font-face {font-family:Verdana; panose-1:2 11 6 4 3 5 4 4 2 4;} &#64;font-face {font-family:""&#64;宋体"; panose-1:2 1 6 0 3 1 1 1 1 1;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; font-size:10.5pt; font-family:"Times New Roman";} /* Page Definitions */ &#64;page {} &#64;page Section1 {size:612.0pt 792.0pt; margin:72.0pt 90.0pt 72.0pt 90.0pt;} div.Section1 {page:Section1;} --> </style> <!--[if gte mso 10]> <style> /* Style Definitions */ table.MsoNormalTable { mso-style-parent:""; font-size:10.0pt; font-family:"Times New Roman";} </style> <![endif]-->

这毫无问题,不过这样单单返回一个对象(DOM节点),用起来并不是太实用和方便。为了要用那节点干点事情,你还将要手工编写不少的代码;另外,对于不同类型浏览器之间的差异,要处理起来可真头大了。

进入Ext.element 对象。元素(element)的的确确是Ext的心脏地带,--无论是访问元素(elements)还是完成一些其他动作,都要涉及它。ElementAPI是整个Ext库的基础,如果你时间不多,只是想了解Ext中的一两个类的话,Element一定是首选!

ID获取一个Ext Element如下(首页ExtStart.htm包含一个divID名字为“myDiv”,然后,在ExtStart.js中加入下列语句):

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->Ext.onReady(function(){varmyDiv=Ext.get('myDiv');});

再回头看看Element对象,发现什么有趣的东东呢?

  • Element包含了常见的DOM方法和属性,提供一个快捷的、统一的、跨浏览器的接口(若使用Element.dom的话,就可以直接访问底层DOM的节点。);
  • Element.get()方法提供内置缓存(Cache),多次访问同一对象效率上有极大优势;
  • 内置常用的DOM节点的动作,并且是跨浏览器的定位的位置、大小、动画、拖放等等(add/remove CSS classes, add/remove event handlers, positioning, sizing, animation, drag/drop)。

这意味着你可用少量的代码来做各种各样的事情,这里仅仅是一个简单的例子(完整的列表在Element API 文档中)。

继续在ExtStart.js中,在刚才我们获取好myDiv的位置中加入:

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->myDiv.highlight();//黄色高亮显示然后渐退
myDiv.addClass('red');//添加自定义CSS类(在ExtStart.css定义)
myDiv.center();//在视图中将元素居中
myDiv.setOpacity(.25);//使元素半透明

<meta content="Word.Document" name="ProgId"><meta content="Microsoft Word 11" name="Generator"><meta content="Microsoft Word 11" name="Originator"><link href="file:///C:%5CDOCUME%7E1%5Cphinecos%5CLOCALS%7E1%5CTemp%5Cmsohtml1%5C01%5Cclip_filelist.xml" rel="File-List"><!--[if gte mso 9]><xml> Normal 0 7.8 磅 0 2 false false false MicrosoftInternetExplorer4 </xml><![endif]--><!--[if gte mso 9]><![endif]--><style> <!-- /* Font Definitions */ &#64;font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1;} &#64;font-face {font-family:Verdana; panose-1:2 11 6 4 3 5 4 4 2 4;} &#64;font-face {font-family:""&#64;宋体"; panose-1:2 1 6 0 3 1 1 1 1 1;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; font-size:10.5pt; font-family:"Times New Roman";} /* Page Definitions */ &#64;page {} &#64;page Section1 {size:612.0pt 792.0pt; margin:72.0pt 90.0pt 72.0pt 90.0pt;} div.Section1 {page:Section1;} --> </style><!--[if gte mso 10]> <style> /* Style Definitions */ table.MsoNormalTable { mso-style-parent:""; font-size:10.0pt; font-family:"Times New Roman";} </style> <![endif]-->

获取多个DOM的节点

通常情况下,想获取多个DOM的节点,难以依靠ID的方式来获取。有可能因为没设置ID,或者你不知道ID,又或者直接用ID方式引用有太多元素了。这种情况下,你就会不用ID来作为获取元素的依据,可能会用属性(attribute)或CSS Classname代替。基于以上的原因,Ext引入了一个异常强大的Dom Selector库,叫做DomQuery

DomQuery可作为单独的库使用,但常用于Ext,你可以在上下文环境中(Context)获取多个元素,然后通过Element接口调用。令人欣喜的是,Element对象本身便有Element.selcect的方法来实现查询,即内部调用DomQuery选取元素。这个简单的例子中,ExtStart.htm包含若干段落(<p>标签),没有一个是有ID的,而你想轻松地通过一次操作马上获取每一段,全体执行它们的动作,可以这样做:

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->//每段高亮显示
Ext.select('p').highlight();

Element.select在这个例子中的方便性显露无疑。它返回一个复合元素,能通过元素接口(Element interface)访问每个元素。这样做的好处是可不用循环和不分别访问每一个元素。

DomQuery的选取参数可以是一段较长的数组,其中包括W3C CSS3 Dom选取器、基本XPatchHTML属性和更多,请参阅DomQuery API文档以了解这功能强大的库个中细节。

响应事件

到这范例为止,我们所写的代码都是放在onReady中,即当页面加载后总会立即执行,功能较单一——这样的话,你便知道,如何响应某个动作或事件来执行你希望做的事情,做法是,先分配一个function,再定义一个event handler事件处理器来响应。我们由这个简单的范例开始,打开ExtStart.js,编辑下列的代码:

functionMyparagraphClick()
{
alert(
"dsdf");
}


functiontest(e)
{
varpara=Ext.get(e.target);
para.highlight();
Ext.MessageBox.show(
{title:'paraclicked',msg:para.dom.innerHTML,width:400,
buttons:Ext.MessageBox.OK,animEl:para}
);
}

Ext.onReady(
function()
{
varmyDiv=Ext.get('myDiv');
myDiv.highlight();
myDiv.addClass(
'red');
myDiv.center();

Ext.select(
'p').highlight();
Ext.get(
'myButton').on('click',function()
{
alert(
"clickme!");
}
);
varparagraphClick=function(e)
{
//alert("clickwto");
Ext.get(e.target).highlight();
}

Ext.select(
'p').on('click',test);

}
);

现在,为了我们接下来阐述新概念的演示,请观察MessageBox的调用。乍一看,这像一连串的参数传入到方法中,但仔细看,这是一个非常特别的语法。实际上,传入到MessageBox.show的只有一个参数:一个Object literal,包含一组属性和属性值。在Javascript中,Object Literal是动态的,你可在任何时候用{}创建一个典型的对象(object)。其中的字符由一系列的name/value组成的属性,属性的格式是[property name]:[property value]。在整个Ext中,你将会经常遇到这种语法,因此你应该马上消化并吸收这个知识点!

使用Object Literal的原因是什么呢?主要的原因是可伸缩性(flexibility的考虑",随时可新增、删除属性,亦可不管顺序地插入。而方法不需要改变。这也是多个参数的情况下,为最终开发者带来不少的方便(本例中的MessageBox.show())。例如,我们说这儿的foo.action方法,有四个参数,而只有一个是你必须传入的。本例中,你想像中的代码可能会是这样的foo.action(null, null, null, 'hello').,若果那方法用Object Literal来写,却是这样, foo.action({ param4: 'hello' }),这更易用和易读.

Grid

GridExt中人们最想先睹为快的和最为流行Widgets之一。好,让我们看看怎么轻松地创建一个Grid并运行。

Ext.onReady(function()
{

varmyData=[['Apple',29.89,0.24,0.81,'9/112:00am'],
[
'Ext',83.81,0.28,0.34,'9/1212:00am'],
[
'Google',71.72,0.02,0.03,'10/112:00am'],
[
'Microsoft',52.55,0.01,0.02,'7/412:00am'],
[
'Yahoo!',29.01,0.42,1.47,'5/2212:00am']];
varmyReader=newExt.data.ArrayReader({},[{name:'company'},{name:'price',type:'float'},{name:'change',type:'float'},{name:'pctChange',type:'float'},{name:'lastChange',type:'date',dateFormat:'n/jh:ia'}]);
vargrid=newExt.grid.GridPanel({store:newExt.data.Store({data:myData,
reader:myReader}
),
columns:[
{header:"Company",width:120,sortable:true,dataIndex:'company'},{header:"Price",width:90,sortable:true,dataIndex:'price'},{header:"Change",width:90,sortable:true,dataIndex:'change'},{header:"%Change",width:90,sortable:true,dataIndex:'pctChange'},{header:"LastUpdated",width:120,sortable:true,
renderer:Ext.util.Format.dateRenderer(
'm/d/Y'),
dataIndex:
'lastChange'}
],
viewConfig:
{forceFit:true},
renderTo:
'content',
title:
'MyFirstGrid',
width:
500,
frame:
true}
);

grid.getSelectionModel().selectFirstRow();
}
);

这看上去很复杂,但实际上加起来,只有四行代码(不包含测试数据的代码)。

  • 第一行创建数组并作为数据源。实际案例中,你很可能从数据库、或者WebService那里得到动态的数据。
  • 接着,我们创建并加载data store data store将会告诉Ext的底层库接手处理和格式化这些数据。不同的数据类型须在类Reader中指明。
  • 接着,我们创建一个Grid的组件,传入各种的配置值,有:
    • 新的data store, 配置好测试数据和reader
    • 列模型column model定义了columns的配置
    • 其他的选择指定了Grid所需的功能
  • 最后,通过SelectionModel告诉Grid高亮显示第一行。

不是太困难吧?如果一切顺利,完成之后你会看到像这样的:


使用Ajax

在弄好一些页面后,你已经懂得在页面和脚本之间的交互(interact)原理。接下来,你应该掌握的是,怎样与远程服务器(remote server)交换数据,常见的是从数据库加载数据(load)或是保存数据(save)到数据库中。通过JavaScript异步无刷新交换数据的这种方式,就是所谓的AjaxExt内建卓越的Ajax支持,例如,一个普遍的用户操作就是,异步发送一些东西到服务器,然后,UI元素根据回应(Response)作出更新。

客户端:

<divid="msg"></div>
<div>Name:<inputtype="text"id="name"/>
<inputtype="button"id="okButton"value="确定"/>
</div>

Ext.onReady(function()
{
Ext.get(
'okButton').on('click',function()
{
varmsg=Ext.get('msg');
msg.load(
{url:'http://localhost:8080/ajaxDemo/hello',
params:
'name='+Ext.get('name').dom.value,
text:
'Updating'}
);
msg.show();
}
);

}
);

服务器端:

importjava.io.IOException;
importjava.io.PrintWriter;

importjavax.servlet.ServletException;
importjavax.servlet.http.HttpServlet;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;


publicclasshelloextendsHttpServlet{
publichello(){
super();
}

publicvoiddestroy(){
super.destroy();//Justputs"destroy"stringinlog
//Putyourcodehere
}

publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)
throwsServletException,IOException{
doPost(request,response);
}


publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)
throwsServletException,IOException{

response.setContentType(
"text/html");
PrintWriterout
=response.getWriter();

out.print(
"FromServer:hello,ajax!");
out.close();
}


publicvoidinit()throwsServletException{
//Putyourcodehere
}


}


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics