I've been looking for examples or tips on adding and deleting items 
from a hierarchical dataprovider. The amount of support documentation 
seems limited, which i suppose is understandable for a beta, even if it 
is Beta3. 

This post from pearls of flex was very helpful.
http://flexpearls.blogspot.com/2007/11/xmllist-hierarchicaldata-and.html

However, I would have imagined that it would have been a relatively 
simple task to delete the selected item from the advanced data grid, 
even if it was heirarchical. Anyway I'll include an example of what i 
tried.

Has anyone else tried anything along this line? Eventually the data 
will be coming back from a service call, but i'm just trying to 
familiarise myself with the advancedDataGrids capabilities  before i 
get into too heavily.

cheers!
Rob

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"; 
layout="absolute">
        <mx:Script>
        <![CDATA[
                import mx.collections.HierarchicalData;
        
        private var cities:Array = [
                { location: 'Group1',
                children: [
                        { location: 'item1',
                        population: 2236765,
                        family: 886053,
                        code: 'TO'
                        },
                        { location: 'item2',
                        population: 183869,
                        family: 75317,
                        code: 'VC'
                        },
                        { location: 'item3',
                        population: 334614,
                        family: 130363,
                        code: 'NO' }
                ]},
                { location: 'Group2',
                children: [
                        { location: 'item1',
                        population: 213587,
                        family: 90934,
                        code: 'IM'
                        },
                        { location: 'item2',
                        population: 284647,
                        family: 120883,
                        code: 'SV'
                        },
                        { location: 'item3',
                        population: 950849,
                        family: 391796,
                        code: 'GE'
                        }
                ]},
                { location: 'Group3',
                children: [
                        { location: 'item1',
                        population: 797039,
                        family: 289925,
                        code: 'VA'
                        },
                        { location: 'item2',
                        population: 522147,
                        family: 188961,
                        code: 'CO'
                        },
                        { location: 'item3',
                        population: 3738685,
                        family: 1423856,
                        code: 'MI'
                        }]
                }];
        
        
                private function addBranchDP():void
            {   
                cities.push({ location: 'Group4',
                        children: [
                        { location: 'Item1',
                        population: 797039,
                        family: 289925,
                        code: 'VA'
                        },
                        { location: 'Item2',
                        population: 522147,
                        family: 188961,
                        code: 'CO'
                        },
                        { location: 'Item3',
                        population: 3738685,
                        family: 1423856,
                        code: 'MI'
                        }]
                        });
                        
                        myADG.dataProvider = new HierarchicalData
(cities);
            }
            
            private function removeBranchDP():void
            {
                // This doesn't work
                myADG.removeChildAt(myADG.selectedIndex);
                
            }
        ]]>

</mx:Script>
<mx:AdvancedDataGrid
        id="myADG"
        it
        dataProvider="{new HierarchicalData(cities)}"
        width="90%"
        variableRowHeight="true"
        wordWrap="true" x="24" y="10" height="400">
        <mx:columns>
        <mx:AdvancedDataGridColumn headerText="Region"
        dataField="location"/>
        <mx:AdvancedDataGridColumn headerText="Population"
        dataField="population" />
        <mx:AdvancedDataGridColumn headerText="Code"
        dataField="code"/>
        <mx:AdvancedDataGridColumn headerText="Family"
        dataField="family"/>
        </mx:columns>
</mx:AdvancedDataGrid>
        <mx:Button x="63" y="418" label="Add" buttonMode="true" 
click="addBranchDP()"/>
        <mx:Button x="120" y="418" label="Delete" buttonMode="true" 
click="removeBranchDP()"/>
        <mx:Text x="541" y="420" text="{myADG.selectedIndex}" 
width="233" color="#FFFFFF"/>
        <mx:Text x="541" y="444" text="{myADG.selectedIndices.valueOf
()}" width="233" color="#FFFFFF"/>
        <mx:Label x="400" y="444" text="myADG.selectedIndex:" 
color="#FFFFFF"/>
        <mx:Label x="400" y="420" text="myADG.selectedIndex:" 
color="#FFFFFF"/>
</mx:Application>

Reply via email to