AdvancedDatagrid Tree view row number

Posted on 2009-04-23
Last Modified: 2013-11-12
How to number the records in AdvancedDatagrid tree view withing each parent group.

In flat view I'm using a label function to produce row numbers but it's not working  for tree view

// Function to produce row numbers for grid
             private function lfRowNum(oItem:Object,iCol:int):String
             var iIndex:int = dataGrid.dataProvider.getItemIndex(oItem) + 1;
             return String(iIndex);

Question by:VKN9876
    LVL 12

    Expert Comment

    You would need a more complex labelFunction for this. The logic would be to extract the oItem parent index and oItem index and subtract one from the other.

    Author Comment

    Do you have a sample?
    LVL 37

    Accepted Solution

    Do you mean something like this?

    * Start up the program
    * press Group
    * and click a node in the tree to open
    * the column "Child Nr" shows the number of the item (given its parent)
    <?xml version="1.0" encoding="utf-8"?>
    <mx:Application xmlns:mx="" layout="absolute" width="1393" height="920" creationComplete="createData()" >
          	import mx.collections.ListCollectionView;
          	import mx.collections.IHierarchicalData;
          	import mx.collections.IHierarchicalCollectionView;
          	import mx.controls.Alert;
            import mx.collections.ArrayCollection;
            import mx.collections.GroupingField;
            import mx.collections.Grouping;
                import mx.collections.GroupingCollection;  
            import mx.controls.advancedDataGridClasses.*;  
            import mx.collections.IViewCursor
            import mx.collections.SummaryField
            import mx.collections.SummaryObject
            import mx.collections.SummaryRow
            [Bindable]private var QuarterResults:ArrayCollection;
            [Bindable] private var _max:int = 0; 
            private var groupingMode:Boolean = false;
            private function createData():void
                  QuarterResults = new ArrayCollection();
                  var j:int = 0
                  var q:int=0
                  for(var i:int=0;i<20;i++)
                        var rnd:* = Math.random()*10000
                        if(i<10) {
                        else {
                        var lic:Number = Math.round((rnd/1000)*rnd)
                        var obj:Object = {Company:"Company"+i,Sales:Math.round(rnd),Licenses:lic,Quarter:"Q"+q}
                  _max = colgrouped_adg.dataProvider.length;
            //Group the flat XML data based on Quarter.
            private function applyGrouping():void
              //Make a grouping collection
              var mygroup:GroupingCollection=new GroupingCollection();
                  //Set the source to the array collection (this case dataProvider)
                mygroup.source = colgrouped_adg.dataProvider;
                //Create a new grouping
                  var group:Grouping = new Grouping();
                  //Create a summary row instance
                  var sr:SummaryRow = new SummaryRow();
                  //Set the summaryObjectFunction
                  //This function tells ADG to add a summary row in addition to the existing data
                  sr.summaryObjectFunction = objFunc;
                  //Set the summary field. The field on which the summary will be calculated.
                  var sf:SummaryField = new SummaryField("Licenses");
                  //Summary Function - the function that calculates the summary row display
                  sf.summaryFunction = func;
                  //set the summary rows fields property to summaryField
                  sr.fields = [sf];
                  //Place the summary row (Last means data First, summary Last!)
                  sr.summaryPlacement = "last";
              //Group on Quarter
                  var gf:GroupingField = new GroupingField("Quarter");
                  var gf1:GroupingField = new GroupingField("Company");
                  //set the summary row(s) to groupingField
                  //Set the fields to the grouping
                  group.fields = [gf,gf1];
                  //Finally, Set the group to the grouping property of groupedCollection
                  mygroup.grouping = group;
                  //Refresh the group
                  //Set the dataProvider to the grouping collection
                  colgrouped_adg.dataProvider = mygroup
                  //Call validateNow() to redraw the dg.
          		  groupingMode = true;
            private function onItemOpenClose():void {
            private function updateItemCount():void {
    	  		_max = IHierarchicalCollectionView(colgrouped_adg.dataProvider).length;
            private function applyUnGrouping():void {
            	colgrouped_adg.dataProvider = QuarterResults;
            	_max = colgrouped_adg.dataProvider.length;
            	groupingMode = false;
            private function func(itr:IViewCursor,field:String, str:String=null):Object
                  var sum:Number=0
                        var value:Number = Number(itr.current.Licenses);
                          sum+= value
                  return sum
            private function objFunc():SummaryObject
            	var obj:SummaryObject = new SummaryObject();
              	obj.summary = true;
              	return obj;
           	private function selectem():void{
                var na:Array = new Array();
                for( var n:int = minv.value; n<= maxv.value; n++)                  
                	na.push( n-1 );
                colgrouped_adg.selectedIndices = na; 
            private function selectAll():void{
                  var na:Array = new Array();
                  for( var i:int=1; i<= _max; i++)                  
                  colgrouped_adg.selectedIndices = na;
            private function deSelectAll():void{
            	colgrouped_adg.selectedItems = null;
            private function getRowNum(item:Object, column:AdvancedDataGridColumn):String {
            	var arr:ArrayCollection = null;
            	if (colgrouped_adg.dataProvider is ArrayCollection) {
            		arr = ArrayCollection(colgrouped_adg.dataProvider);
            		var index:int = arr.getItemIndex(item);
           			return index==-1 ? "" : index+1+"";
            	else if (colgrouped_adg.dataProvider is IHierarchicalCollectionView) {
            		var depth:int = IHierarchicalCollectionView(colgrouped_adg.dataProvider).getNodeDepth(item);
            		if (depth==1)
            			return "";
            		var parent:Object = IHierarchicalCollectionView(colgrouped_adg.dataProvider).getParentItem(item);
            		var childrenList:ListCollectionView = IHierarchicalCollectionView(colgrouped_adg.dataProvider).getChildren(parent) as ListCollectionView;
            		return (childrenList.getItemIndex(item)+1)+"";
            	return "";
     <mx:AdvancedDataGrid width="600" height="300" dataProvider="{QuarterResults}" id="colgrouped_adg" y="50" 
     	itemOpen="onItemOpenClose()" itemClose="onItemOpenClose()">
                         <mx:AdvancedDataGridColumn dataField="Company"/>
                         <mx:AdvancedDataGridColumn dataField="Sales"/>
                         <mx:AdvancedDataGridColumn dataField="Quarter"/>
                         <mx:AdvancedDataGridColumn dataField="Licenses"/>
                         <mx:AdvancedDataGridColumn labelFunction="getRowNum" headerText="Child Nr"/>
            <mx:Button label="Group" click="applyGrouping()"/>
            <mx:Button label="Remove Group" click="applyUnGrouping()"/>
        <mx:Button label="Select All" width="100" click="selectAll()"/>
                <mx:Button label="Deselect All" width="100" click="deSelectAll()"/>
                      <mx:Label text="Select Records From"/>
          <mx:NumericStepper  id="minv"   minimum="1" maximum="{maxv.value}" change="selectem()"/>
           <mx:Label text="To"/>
          <mx:NumericStepper id="maxv"  minimum="{minv.value}" maximum="{_max}"   change="selectem()" />

    Open in new window


    Author Closing Comment

    Thanks. Great job
    LVL 37

    Expert Comment

    Thanx 4 axxepting

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    This article provides a case study on how our local youth baseball league deployed a new website, including the platform selection, implementation and benefits to the league.
    Objective of This Article In 1990’s, when I was a budding software professional, I had a lot of confusion about which stream or technology, I had to choose to build my career. In those days, I had lot of confusion like whether to choose System so…
    The goal of the tutorial is to teach the user how to select the video input device. Make sure you have an input device that in connected and work and recognized by Adobe Flash Media Live Encoder and select it in the “video input” menu.
    This Micro Tutorial will teach to how to utilize bit rate in Adobe Flash Media Live Encoder.

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now