Flex中的DataGrid小结(1)-对数据的操作
Flex中的DataGrid总结(1)-对数据的操作
DataGrid是Flex中的一个高级表格控件
1、操作指定数据
刚开始接触项目中的Flex时, 时常为操作DataGrid感到郁闷, 因为根本就没办法拿到DataGrid的每一列数据的对象, 于是对操作DataGrid的数据就感到麻木了。
但这是一种错误的想法, 在Flex中操作DataGrid的数据是通过操作数据源来实现的, 即是通过其dataProvider属性来的。
DataGrid中的dataProvider可以由数组或集合进行指定,一旦指定后Flex会遍历其中所有元素,并将其显示在DataGrid里。由此可见,我们操作DataGrid实质还是操作数组或集合。
这么一想,那我们就必须能取得数组(或集合)的索引才能对其进行操作。
获取当前索引最典型的应用应该就是复制选中的一行数据吧!
一但拿到索引后,我们可以尽情的更新指定行或指定单元格的数据吧!
//====================华丽的分割线============================================
2、给DataGrid加上其它组件
刚开始的时候,项目里需要加复选框, 当时真是不知所措,但现在看起来也太easy了
主要是itemRenderer属性,用它可以很方便的嵌套各各组件,好了,今天就分享改得到此了, 给大家一个问题哦,怎样在嵌套了复选框的DataGrid中获取所有被选中的数据到另一个DataGrid中呢?欲知详情请 留意下回 《Flex中的DataGrid总结(2)》
DataGrid是Flex中的一个高级表格控件
1、操作指定数据
刚开始接触项目中的Flex时, 时常为操作DataGrid感到郁闷, 因为根本就没办法拿到DataGrid的每一列数据的对象, 于是对操作DataGrid的数据就感到麻木了。
DataGrid中的dataProvider可以由数组或集合进行指定,一旦指定后Flex会遍历其中所有元素,并将其显示在DataGrid里。由此可见,我们操作DataGrid实质还是操作数组或集合。
这么一想,那我们就必须能取得数组(或集合)的索引才能对其进行操作。
<fx:Script> <![CDATA[ import mx.collections.ArrayCollection; import mx.controls.Alert; [Bindable] private var myData:ArrayCollection = new ArrayCollection( [{"index":1, "name":"aa", "gender":"男", "age":12}, {"index":2, "name":"bb", "gender":"男", "age":12}, {"index":3, "name":"cc", "gender":"男", "age":12}, {"index":4, "name":"dd", "gender":"男", "age":12}, {"index":5, "name":"ee", "gender":"男", "age":12} ] ); private function showIndex(): void{ Alert.show("我的索引是" + data.selectedIndex.toString() );//selectedIndex方法返回当前选中的索引 } </fx:Script> <mx:DataGrid id="data" dataProvider="{myData}" doubleClickEnabled="true" doubleClick="showIndex(event)" editable="true" > <mx:columns> <mx:DataGridColumn headerText="序号" dataField="index" editable="false" /> <mx:DataGridColumn headerText="姓名" dataField="name" /> <mx:DataGridColumn headerText="性别" dataField="gender" /> <mx:DataGridColumn headerText="年龄" dataField="age" editable="true" editorDataField="value" > <mx:itemEditor> <fx:Component> <mx:NumericStepper maximum="1000000" /> </fx:Component> </mx:itemEditor> </mx:DataGridColumn> </mx:columns> </mx:DataGrid>
获取当前索引最典型的应用应该就是复制选中的一行数据吧!
一但拿到索引后,我们可以尽情的更新指定行或指定单元格的数据吧!
//====================华丽的分割线============================================
2、给DataGrid加上其它组件
刚开始的时候,项目里需要加复选框, 当时真是不知所措,但现在看起来也太easy了
<mx:columns> <mx:DataGridColumn headerText="选中"> <mx:itemRenderer> <fx:Component> <s:CheckBox /> </fx:Component> </mx:itemRenderer> </mx:DataGridColumn> <mx:DataGridColumn headerText="序号" dataField="index" editable="false" /> <mx:DataGridColumn headerText="姓名" dataField="name" /> <mx:DataGridColumn headerText="性别" dataField="gender" /> <mx:DataGridColumn headerText="年龄" dataField="age" editable="true" editorDataField="value" > <mx:itemEditor> <fx:Component> <mx:NumericStepper maximum="1000000" /> </fx:Component> </mx:itemEditor> </mx:DataGridColumn> </mx:columns>
主要是itemRenderer属性,用它可以很方便的嵌套各各组件,好了,今天就分享改得到此了, 给大家一个问题哦,怎样在嵌套了复选框的DataGrid中获取所有被选中的数据到另一个DataGrid中呢?欲知详情请 留意下回 《Flex中的DataGrid总结(2)》