11 Replies Latest reply on Mar 13, 2008 9:48 PM by Asha_Athrey

    Data behind the new Advanced Datagrid Control

    graphicsxp
      Hi,
      I've used the AdvancedDataGrid control along with a GroupingCollection and a SummaryField. I've assigned a double click event handler to the grid and now I'm trying to figure out how to get the data behind the row that was clicked and if it's a group or a leaf.
      I've seen the dataProvider.treeData property but I don't understand how to find my data based on the selected index (by the way why is there no SelectedIndex property, I can only see a selectedCells property...)

      Here's my code, can you help ?
        • 1. Re: Data behind the new Advanced Datagrid Control
          sameerb
          Here is a sample which might be useful -

          <?xml version="1.0" encoding="utf-8"?>
          <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="vertical">

          <mx:Script>
          <![CDATA[
          import mx.collections.IHierarchicalCollectionView;

          private function doubleClickHandler():void
          {
          var item:Object = adg.selectedItem;
          var index:int = adg.selectedIndex;

          if (!item || index == -1)
          return ;

          trace("Double Clicked item index:" + index);

          if (IHierarchicalCollectionView(adg.dataProvider).getParentItem(item) == null)
          trace("Group Row");
          else
          trace("Leaf Row");

          trace("Depth of the item:" + IHierarchicalCollectionView(adg.dataProvider).getNodeDepth(item));
          }
          ]]>
          </mx:Script>

          <mx:Array id="arr">
          <mx:Object From="Phill" Subject="GMC-1 Release" Date="12/08/06" CC="Jim" Profit="69" />
          <mx:Object From="Phill" Subject="GMC-1 Release1" Date="12/08/06" CC="Jim" Profit="31" />
          <mx:Object From="Harry" Subject="GMC-1 Release3" Date="November 2, 2006" CC="Ram" Profit="10" />
          <mx:Object From="Barb" Subject="GMC-1 Release4" Date="12/08/06" CC="Anant" Profit="20" />
          <mx:Object From="Amit" Subject="GMC-1 Release2" Date="12/07/06" CC="Jim" Profit="28" />
          <mx:Object From="John" Subject="GMC-1 Release2" Date="12/07/06" CC="Jim" Profit="12" />
          <mx:Object From="Amit" Subject="GMC-1 Release21" Date="12/07/06" CC="Jim" Profit="22" />
          </mx:Array>

          <mx:AdvancedDataGrid id="adg" doubleClickEnabled="true" doubleClick="doubleClickHandler()"
          creationComplete="gc.refresh();"
          height="300">
          <mx:dataProvider>
          <mx:GroupingCollection id="gc" source="{arr}" >
          <mx:Grouping>
          <mx:GroupingField name="From" >
          <mx:SummaryRow summaryPlacement="group">
          <mx:fields>
          <mx:SummaryField dataField="Profit" operation="COUNT" label="summary" />
          </mx:fields>
          </mx:SummaryRow>
          </mx:GroupingField>
          </mx:Grouping>
          </mx:GroupingCollection>
          </mx:dataProvider>

          <mx:columns>
          <mx:AdvancedDataGridColumn dataField="From"/>
          <mx:AdvancedDataGridColumn dataField="Subject"/>
          <mx:AdvancedDataGridColumn dataField="Date"/>
          <mx:AdvancedDataGridColumn id="summaryColumn" dataField="Profit"/>
          </mx:columns>
          </mx:AdvancedDataGrid>

          </mx:Application>
          • 2. Re: Data behind the new Advanced Datagrid Control
            graphicsxp Level 1
            Hello Sameerb,

            Your sample gives me indexes and depth but I still can't see how to use this information in order to find out which node was clicked.
            For example if you click on the second row under 'Amit', it gives 2 for the selected index and 2 for the depth. But on which object should I use this details.
            Ideally when the user clicks on a row, I'd like to display the row details in a popup. Can you show me how to get the row details as opposed to the index, depth etc...

            Thanks
            • 3. Re: Data behind the new Advanced Datagrid Control
              sameerb Level 1
              adg.selectedItem will give the Object.
              • 4. Re: Data behind the new Advanced Datagrid Control
                graphicsxp Level 1
                Yes and I can get all the data I need from it BUT there should be a way to reference this object by finding it in the datasource using the selected index , or is it not possible ?
                • 5. Re: Data behind the new Advanced Datagrid Control
                  sameerb Level 1
                  index can be used to refer in the dataProvider -

                  var index:int = adg.selectedIndex;

                  var cursor:IViewCursor = IHierarchicalCollectionView(adg.dataProvider).createCursor();
                  cursor.seek(CursorBookmark.FIRST, index);

                  var itemFromCursor:Object = cursor.current;

                  Note: This will work only in case of hierarchical data.
                  • 6. Re: Data behind the new Advanced Datagrid Control
                    graphicsxp Level 1
                    Thanks. You've helped me a lot.
                    • 7. Re: Data behind the new Advanced Datagrid Control
                      core009
                      Sameer, can you elaborate on the example given above. I still cannot seem to find the value of the Group Row.

                      I've added:
                      var item:Object = adg.selectedItem;
                      Alert.show("Double Clicked item:" + item);

                      but the alert just has Object:object instead of the actual value. what am i missing?

                      Thanks
                      • 8. Re: Data behind the new Advanced Datagrid Control
                        graphicsxp Level 1
                        Hi Core009,

                        I've you tried Sameerb's code ? It worked for me. Doing dg.selectedItem will not give you any result with groups you are using a hierarchical view. Therefore you need to cast the dataProvider of you grid, and create a cursor to navigate through the records.

                        Hope this helps
                        • 9. Re: Data behind the new Advanced Datagrid Control
                          sameerb Level 1
                          item will be the Object.
                          You can get the group name in
                          item["GroupLabel"]

                          Use -
                          Alert.show("Double Clicked item:" + item["GroupLabel"]);

                          Note: The property GroupLabel will only be present in the Group row's Object.
                          • 10. Re: Data behind the new Advanced Datagrid Control
                            507Coder
                            Hi. so the property treeData is not accesible directly?
                            • 11. Re: Data behind the new Advanced Datagrid Control
                              Asha_Athrey
                              Hi....

                              I want to select a single cell and perform some operation depending on the datafield of the cell or the data of the cell. how can i access the data in a particular cell of a datagrid using itemClick evnt.