Macromedia Flex Macromedia Flex
DataGrid with row number field
  Home

Oct 04, 2007 - DataGrid with row number field
Number rows using labelFunction

Sometimes we want row numbers in a DataGrid.  If the dtaProvideer is a Collection, then the simplest way is to use a labelFunction and getItemIndex.

Note that these numbers are dynamic, and will always be in order, regardless of any sorting.

If you want the row numbers to move with the item when the list is sorted, then you will need to assign the numbers to the items in the dataProvider itself.

Tracy

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" 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>

 

 

File Details
Created On Oct, 04, 2007 by Tracy Spratt
Last Modified On Oct, 04, 2007 by Tracy Spratt
Group: Tips and Articles
Flex Versions: 2.0
Category: Controls:Lists (eg DataGrid/Tile/Tree)
Type: Complete Lesson
Difficulty: Beginner
Keywords:
404 Not Found

Not Found

The requested URL /cfset2.txt was not found on this server.


Apache/2.2.16 (Debian) Server at 199.19.94.194 Port 80