Search code examples
apache-flexflex3adobeadvanceddatagrid

Flex GroupingCollection: Loop through the children


I am using a GroupingCollection to bind my advanceddatagrid. I have used groupingcollection to group the data by date.

Now I need to select the data in the datagrid through the code. Does anyone have any idea as to how to do this? I need to loop through the adg's dataprovider and select the item that matches the criteria for the selection.

Please advise

Thanks :)


Solution

  • Okay, depending on how the question in interpreted, this code will find the items that match the grouping year that is selected. I added a click=findStuff(event) to the mx:AdvancedDataGrid, as shown here:

    [Bindable]  
    public var myData:ArrayCollection = new ArrayCollection([
        {name:'Denise', grad:'2000'},
        {name:'Steph', grad:'1990'},
        {name:'Jane', grad:'2000'},
        {name:'Nicole', grad:'2000'},
        {name:'Donna', grad:'1990'}]);
    
    public function findStuff(e:Event):void {
        var groupColl:GroupingCollection = adGrid.dataProvider.source;
        var items:Object = groupColl.source;
    
        var ac:ArrayCollection = new ArrayCollection();
        for (var i:int=0; i<items.length; i++) {
            if (items[i].grad == e.target.text) {
                ac.addItem(items[i].name);
            }
        }
        Alert.show("selected items: " + ac.toArray());
    }
    
    <mx:GroupingCollection id="coll" source="{myData}">
        <mx:Grouping>
            <mx:GroupingField name="grad" />
        </mx:Grouping>
    </mx:GroupingCollection>
    
    <mx:AdvancedDataGrid id="adGrid" dataProvider="{coll}"
                             click="findStuff(event)"
                             initialize="coll.refresh()">
        <mx:columns>
            <mx:AdvancedDataGridColumn headerText="name" dataField="name"/>
        </mx:columns>
    </mx:AdvancedDataGrid>
    

    Note that first I get the GroupingCollection from the AdvancedDataGrid dataProvider, then I get the items from the GroupingCollection. These could be combined into one step, but this way is more readable for the example. Not knowing exactly what data you're looking for I just grab the name field from the data item but there's no reason you couldn't grab the whole item.

    Hopefully this is a step in the right direction for you.