[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3132
  • Last Modified:

AdvancedDatagrid Tree view row number

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);
         }

Thanks
Vimal
0
VKN9876
Asked:
VKN9876
  • 2
  • 2
1 Solution
 
lexxwernCommented:
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.
0
 
VKN9876Author Commented:
Do you have a sample?
0
 
zzynxSoftware engineerCommented:
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="http://www.adobe.com/2006/mxml" layout="absolute" width="1393" height="920" creationComplete="createData()" >
 
<mx:Script>
      <![CDATA[
      	import mx.collections.ListCollectionView;
      	import mx.collections.IHierarchicalData;
      	import mx.collections.IHierarchicalCollectionView;
      	import mx.controls.Alert;
      	import mx.events.ListEvent;
        //Imports
        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) {
                    	q=1
                    }
                    else {
                        q=2
                    }
                    var lic:Number = Math.round((rnd/1000)*rnd)
                    var obj:Object = {Company:"Company"+i,Sales:Math.round(rnd),Licenses:lic,Quarter:"Q"+q}
                    QuarterResults.addItem(obj);
              }
              
              _max = colgrouped_adg.dataProvider.length;
              //applyGrouping()
        }
        //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
              gf.summaries=[sr]
              //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
              mygroup.refresh();
              //Set the dataProvider to the grouping collection
              colgrouped_adg.dataProvider = mygroup
              //Call validateNow() to redraw the dg.
              colgrouped_adg.validateNow()
      
      		  updateItemCount();
      		  groupingMode = true;
        }
        
        private function onItemOpenClose():void {
        	updateItemCount();
        }
        
        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
              while(!itr.afterLast)
              {
                    //trace(itr.current)
                    var value:Number = Number(itr.current.Licenses);
                    if(!isNaN(value))
                    {
                      sum+= value
                    }
                    itr.moveNext()
              }
              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++)                  
                    na.push(i-1);
              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:Script>
<mx:VBox>
      
 
 <mx:AdvancedDataGrid width="600" height="300" dataProvider="{QuarterResults}" id="colgrouped_adg" y="50" 
 	itemOpen="onItemOpenClose()" itemClose="onItemOpenClose()">
         <mx:columns>
                     <mx:AdvancedDataGridColumn dataField="Company"/>
                     <mx:AdvancedDataGridColumn dataField="Sales"/>
                     <mx:AdvancedDataGridColumn dataField="Quarter"/>
                     <mx:AdvancedDataGridColumn dataField="Licenses"/>
                     <mx:AdvancedDataGridColumn labelFunction="getRowNum" headerText="Child Nr"/>
         </mx:columns>         
  </mx:AdvancedDataGrid>
  <mx:HBox>
        <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()" />
 
  </mx:HBox>
  </mx:VBox>
</mx:Application>

Open in new window

0
 
VKN9876Author Commented:
Thanks. Great job
0
 
zzynxSoftware engineerCommented:
Thanx 4 axxepting
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now