1

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 :)

Josh
  • 10,961
  • 11
  • 65
  • 108
online19
  • 125
  • 5
  • 17

2 Answers2

1

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.

dustmachine
  • 10,622
  • 5
  • 26
  • 28
  • Thanks dustmachine. I will try this one. The scenario is, I have an arraycollection bound to a grid. My app has an option to group by date. Now, when I group by date, I change the dataprovider to groupingcollection. So, when my dataprovider is changed, I wanted to maintain the state of the selected item in the grid. So that when I change the dataprovider, I can programatically select the item in the grid. I will give this one a try – online19 Nov 18 '09 at 15:40
  • You could try to manipulate the adGrid to highlight the proper rows by sticking `adGrid.selectedItems = ac.toArray();` just above the `Alert.show` in the above code. If the rows/items are in a tree/folder that's closed then they probably won't show... An upvote would be appreciated if this solves it for you. – dustmachine Nov 18 '09 at 18:55
  • Sure. Let me try this. Thanks :) – online19 Nov 23 '09 at 18:53
0

The source property should contain the flat representation of the data.

CookieOfFortune
  • 13,836
  • 8
  • 42
  • 58
  • Yes. Grouping collection is bound to the arraycollection. its a flat list of objects. But, how should I loop and select the desired child? You have any clue about it? – online19 Nov 13 '09 at 20:49
  • you can just loop through it until it matches what you have selected. – CookieOfFortune Nov 16 '09 at 18:29