有些时候,我们需要在dataGrid中添加记录行数的列。如果你使用的dataProvider是Collection,可以使用labelFunction和getItemIndex实现。不过这种方法会使得显示的行数是动态的,它不会随着其他列的排序(sort)而变化,如果你非要这样实现,需要把行数作为dataProvider的一部分了。
<?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="" layout="absolute" creationComplete="initApp()"> <mx:Script><![CDATA[ import mx.collections.ArrayCollection; import mx.controls.Alert; [Bindable]private var _acDP:ArrayCollection; public function initApp():void { var oItem:Object; _acDP = new ArrayCollection(); oItem = {Album:"The Lost Chord", Artist:"The Moody Blues", Price:"15.99" }; _acDP.addItem(oItem); oItem = {Album:"Meddle", Artist:"Pink Floyd", Price:"17.99" }; _acDP.addItem(oItem); oItem = {Album:"Trespass", Artist:"Genesis", Price:"18.99" }; _acDP.addItem(oItem); }//initApp
private function lfRowNum(oItem:Object,iCol:int):String { var iIndex:int = _acDP.getItemIndex(oItem) + 1; return String(iIndex); } ]]></mx:Script>
<mx:VBox> <mx:DataGrid id="dgSource" dataProvider="{_acDP}" editable="true" > <mx:columns> <mx:Array> <mx:DataGridColumn headerText="Row#" labelFunction="lfRowNum"/> <mx:DataGridColumn headerText="Album" dataField="Album" /> <mx:DataGridColumn headerText="Artist" dataField="Artist" /> <mx:DataGridColumn headerText="Price" dataField="Price" /> </mx:Array> </mx:columns> </mx:DataGrid> </mx:VBox> </mx:Application>
|
至于效果,你可以编译执行。
阅读(4580) | 评论(0) | 转发(0) |