|
首页 | 名片设计 CorelDRAW Illustrator AuotoCAD Painter 其他软件 Photoshop Fireworks Flash |
|
Flex2中,DataGrid假如我们没有指定columns认识的话,DataGrid会自动根据dataProvider的各行数据的属性名隐式自动地创建列,例如如下代码: <?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.macromedia.com/2005/mxml" xmlns="*" layout="vertical" creationComplete="loadDgView()"> <mx:Script> <![CDATA[ [Bindable] public var works:Array= [ { id: 1, name: ’feiy’, sex: ’male’}, { id: 2, name: ’wenj’, sex: ’fmale’}]; private var worksColumns:Array= [ { columnName: "id",headerText: "work’s id",width: 100}, { columnName: "name",headerText: "work’s name",width: 200}, { columnName: "sex",headerText: "work’s sex",width: 100}]; [Bindable] public var departments:Array=[ { id: 1, name: ’tech dept.’}, { id: 2, name: ’service dept.’}]; private var departmentsColumns:Array= [ { columnName: "id",headerText: "department’s id",width: 200}, { columnName: "name",headerText: "department’s name",width:200}]; private function loadDgView(){ if(type_cb.selectedIndex==0){ view_dg.dataProvider=works; }else{ view_dg.dataProvider=departments; } } ]]> </mx:Script> <mx:HBox> <mx:ComboBox id="type_cb" change="loadDgView()"> <mx:dataProvider> <mx:Array> <mx:Object label="works"/> <mx:Object label="departments"/> </mx:Array> </mx:dataProvider> </mx:ComboBox> </mx:HBox> <mx:DataGrid id="view_dg" /> </mx:Application> 当我们选择works时,DataGrid自动根据dataProvider:works数组中的数据生成三列的DataGridColumn;而选择departments时,又相应的变更为两列,异常的方便。 <?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.macromedia.com/2005/mxml" xmlns="*" layout="vertical" creationComplete="loadDgView()"> <mx:Script> <![CDATA[ import mx.controls.gridclasses.DataGridColumn; [Bindable] public var works:Array= [ { id: 1, name: ’feiy’, sex: ’male’}, { id: 2, name: ’wenj’, sex: ’fmale’}]; private var worksColumns:Array= [ { columnName: "id",headerText: "work’s id",width: 100}, { columnName: "name",headerText: "work’s name",width: 200}, { columnName: "sex",headerText: "work’s sex",width: 100}]; [Bindable] public var departments:Array=[ { id: 1, name: ’tech dept.’}, { id: 2, name: ’service dept.’}]; private var departmentsColumns:Array= [ { columnName: "id",headerText: "department’s id",width: 200}, { columnName: "name",headerText: "department’s name",width:200}]; private function loadDgView(){ if(type_cb.selectedIndex==0){ var columns:Array=new Array(); for(var i:int;i<worksColumns.length;i++){ var item:Object=worksColumns[i]; var dgColumn:DataGridColumn=new DataGridColumn(); dgColumn.columnName=item.columnName; dgColumn.headerText=item.headerText; dgColumn.width=item.width; columns.push(dgColumn); } view_dg.columns=columns; view_dg.dataProvider=works; }else{ var columns:Array=new Array(); for(var i:int;i<departmentsColumns.length;i++){ var item:Object=departmentsColumns[i]; var dgColumn:DataGridColumn=new DataGridColumn(); dgColumn.columnName=item.columnName; dgColumn.headerText=item.headerText; dgColumn.width=item.width; columns.push(dgColumn); } view_dg.columns=columns; view_dg.dataProvider=departments; } } ]]> </mx:Script> <mx:HBox> <mx:ComboBox id="type_cb" change="loadDgView()"> <mx:dataProvider> <mx:Array> <mx:Object label="works"/> <mx:Object label="departments"/> </mx:Array> </mx:dataProvider> </mx:ComboBox> </mx:HBox> <mx:DataGrid id="view_dg" /> </mx:Application> 红色部分为增加的代码,相对之前的代码,我们增加了两个数组:worksColumns和departmentsColumns,分别存放相应数据的DataGridColumn属性,然后在loadDgView函数中,根据相应的Column数组创建相应的DataGridColumn数组,最后将其赋值给view_db.columns。 返回类别: Flash教程 上一教程: Flash AS创作:计算器 下一教程: Flash视频教程:太委屈 您可以阅读与"Flex2 发现之旅:动态创建DataGrid列"相关的教程: · Flex2 发现之旅:构建多语言本地化Flex应用 · 枫桥夜泊下 遮罩效果的动态文本 · 净化Flash MX里的动态文本 · Flash 动态遮罩的原理和应用 · Flash通过外部文本动态载入图片 |
快精灵印艺坊 版权所有 | 首页会员中心在线印刷在线编辑付款方式索取样品设计指南连锁门店 |