0 Replies Latest reply on Apr 8, 2009 8:00 AM by irislpc

    get rowIndex for a particular item in ADG

    irislpc

      I have an advancedDataGrid which was populated with a hierarchical arrayCollection. Because the second level contains a long list, I want the header be repeated before each parent node. Since flex doesn't have a built-in function for repeated headers, I need to insert before a row when users click a parent node and style it so that it looks like a header. To change the background color for a particular row, I override the drawRowBackground function in the advancedDataGrid. However, I couldn't find out the exact rowIndex for my faked repeated header. Does anyone know which method I should use if I want to get the rowIndex for a particular item.

       

      http://weblogs.macromedia.com/pent/archives/2007/02/coloring_the_ba.html

       

      The above link gives me a rough idea of how to override the drawRowBackground function. But sine my dataProvider is a hierarchical arrayCollection, I couldn't use getItemAt(dataIndex) to check the property of an item. Any ideas of how to resolve the issue? Many many thanks.

       

      The following is what I have done so far.

       

      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical"
          creationComplete="initListeners()" xmlns:comp="custom_comp.*">
         
          <mx:Script>
              <![CDATA[
                  import mx.controls.advancedDataGridClasses.AdvancedDataGridColumn;
                  import mx.collections.ArrayCollection;
                  import mx.events.AdvancedDataGridEvent;

       


                  [Bindable]
                  private var dpHierarchy:ArrayCollection = new ArrayCollection([

       

                       {trim:"V8 2WD", year_99:"5", year_00:"5", year_01:"5", year_02:"5", year_03:"5",
                                        year_04:"5", year_05:"5", year_06:"5", year_07:"5", year_08:"5", year_09:"5", children: [
                                      {trim:"Engine Major", year_99:"5", year_00:"5", year_01:"5", year_02:"5", year_03:"5",
                                       year_04:"5", year_05:"5", year_06:"5", year_07:"5", year_08:"5", year_09:"5"},
                                      {trim:"Engine Minor", year_99:"5", year_00:"5", year_01:"5", year_02:"5", year_03:"5",
                                       year_04:"5", year_05:"5", year_06:"5", year_07:"5", year_08:"5", year_09:"5"},
                                      {trim:"Engine Cooling", year_99:"5", year_00:"5", year_01:"5", year_02:"5", year_03:"5",
                                       year_04:"5", year_05:"5", year_06:"5", year_07:"5", year_08:"5", year_09:"5"},
                                      {trim:"Transmission Major", year_99:"5", year_00:"5", year_01:"5", year_02:"5", year_03:"5",
                                       year_04:"5", year_05:"5", year_06:"5", year_07:"5", year_08:"5", year_09:"5"},
                                      {trim:"Transmission Minor", year_99:"5", year_00:"5", year_01:"5", year_02:"5", year_03:"5",
                                       year_04:"5", year_05:"5", year_06:"5", year_07:"5", year_08:"5", year_09:"5"},
                                      {trim:"Drive System", year_99:"5", year_00:"5", year_01:"5", year_02:"5", year_03:"5",
                                       year_04:"5", year_05:"5", year_06:"5", year_07:"5", year_08:"5", year_09:"5"},
                                      {trim:"Fuel System", year_99:"5", year_00:"5", year_01:"5", year_02:"5", year_03:"5",
                                       year_04:"5", year_05:"5", year_06:"5", year_07:"5", year_08:"5", year_09:"5"},
                                      {trim:"Electrical System", year_99:"5", year_00:"5", year_01:"5", year_02:"5", year_03:"5",
                                       year_04:"5", year_05:"5", year_06:"5", year_07:"5", year_08:"5", year_09:"5"},
                                      {trim:"Climate System", year_99:"5", year_00:"5", year_01:"5", year_02:"5", year_03:"5",
                                       year_04:"5", year_05:"5", year_06:"5", year_07:"5", year_08:"5", year_09:"5"},
                                      {trim:"Suspension", year_99:"5", year_00:"5", year_01:"5", year_02:"5", year_03:"5",
                                       year_04:"5", year_05:"5", year_06:"5", year_07:"5", year_08:"5", year_09:"5"},
                                      {trim:"Brakes", year_99:"5", year_00:"5", year_01:"5", year_02:"5", year_03:"5",
                                       year_04:"5", year_05:"5", year_06:"5", year_07:"5", year_08:"5", year_09:"5"},
                                      {trim:"Exhaust", year_99:"5", year_00:"5", year_01:"5", year_02:"5", year_03:"5",
                                       year_04:"5", year_05:"5", year_06:"5", year_07:"5", year_08:"5", year_09:"5"},
                                      {trim:"Paint/Trim", year_99:"5", year_00:"5", year_01:"5", year_02:"5", year_03:"5",
                                       year_04:"5", year_05:"5", year_06:"5", year_07:"5", year_08:"5", year_09:"5"},
                                      {trim:"Squeaks & Rattles", year_99:"5", year_00:"5", year_01:"5", year_02:"5", year_03:"5",
                                       year_04:"5", year_05:"5", year_06:"5", year_07:"5", year_08:"5", year_09:"5"},
                                      {trim:"Body Hardware", year_99:"5", year_00:"5", year_01:"5", year_02:"5", year_03:"5",
                                       year_04:"5", year_05:"5", year_06:"5", year_07:"5", year_08:"5", year_09:"5"},
                                      {trim:"Power Equip.", year_99:"5", year_00:"5", year_01:"5", year_02:"5", year_03:"5",
                                       year_04:"5", year_05:"5", year_06:"5", year_07:"5", year_08:"5", year_09:"5"},
                                      {trim:"Audio System", year_99:"5", year_00:"5", year_01:"5", year_02:"5", year_03:"5",
                                       year_04:"5", year_05:"5", year_06:"5", year_07:"5", year_08:"5"}]},
                      
                      
                         
                       {trim:"V6 2WD", year_99:"5", year_00:"5", year_01:"5", year_02:"5", year_03:"5",
                                        year_04:"5", year_05:"5", year_06:"5", year_07:"5", year_08:"5", year_09:"5", children: [
                                      {trim:"Engine Major", year_99:"5", year_00:"5", year_01:"5", year_02:"5", year_03:"5",
                                       year_04:"5", year_05:"5", year_06:"5", year_07:"5", year_08:"5", year_09:"5"},
                                      {trim:"Engine Minor", year_99:"5", year_00:"5", year_01:"5", year_02:"5", year_03:"5",
                                       year_04:"5", year_05:"5", year_06:"5", year_07:"5", year_08:"5", year_09:"5"},
                                      {trim:"Engine Cooling", year_99:"5", year_00:"5", year_01:"5", year_02:"5", year_03:"5",
                                       year_04:"5", year_05:"5", year_06:"5", year_07:"5", year_08:"5", year_09:"5"},
                                      {trim:"Transmission Major", year_99:"5", year_00:"5", year_01:"5", year_02:"5", year_03:"5",
                                       year_04:"5", year_05:"5", year_06:"5", year_07:"5", year_08:"5", year_09:"5"},
                                      {trim:"Transmission Minor", year_99:"5", year_00:"5", year_01:"5", year_02:"5", year_03:"5",
                                       year_04:"5", year_05:"5", year_06:"5", year_07:"5", year_08:"5", year_09:"5"},
                                      {trim:"Drive System", year_99:"5", year_00:"5", year_01:"5", year_02:"5", year_03:"5",
                                       year_04:"5", year_05:"5", year_06:"5", year_07:"5", year_08:"5", year_09:"5"},
                                      {trim:"Fuel System", year_99:"5", year_00:"5", year_01:"5", year_02:"5", year_03:"5",
                                       year_04:"5", year_05:"5", year_06:"5", year_07:"5", year_08:"5", year_09:"5"},
                                      {trim:"Electrical System", year_99:"5", year_00:"5", year_01:"5", year_02:"5", year_03:"5",
                                       year_04:"5", year_05:"5", year_06:"5", year_07:"5", year_08:"5", year_09:"5"},
                                      {trim:"Climate System", year_99:"5", year_00:"5", year_01:"5", year_02:"5", year_03:"5",
                                       year_04:"5", year_05:"5", year_06:"5", year_07:"5", year_08:"5", year_09:"5"},
                                      {trim:"Suspension", year_99:"5", year_00:"5", year_01:"5", year_02:"5", year_03:"5",
                                       year_04:"5", year_05:"5", year_06:"5", year_07:"5", year_08:"5", year_09:"5"},
                                      {trim:"Brakes", year_99:"5", year_00:"5", year_01:"5", year_02:"5", year_03:"5",
                                       year_04:"5", year_05:"5", year_06:"5", year_07:"5", year_08:"5", year_09:"5"},
                                      {trim:"Exhaust", year_99:"5", year_00:"5", year_01:"5", year_02:"5", year_03:"5",
                                       year_04:"5", year_05:"5", year_06:"5", year_07:"5", year_08:"5", year_09:"5"},
                                      {trim:"Paint/Trim", year_99:"5", year_00:"5", year_01:"5", year_02:"5", year_03:"5",
                                       year_04:"5", year_05:"5", year_06:"5", year_07:"5", year_08:"5", year_09:"5"},
                                      {trim:"Squeaks & Rattles", year_99:"5", year_00:"5", year_01:"5", year_02:"5", year_03:"5",
                                       year_04:"5", year_05:"5", year_06:"5", year_07:"5", year_08:"5", year_09:"5"},
                                      {trim:"Body Hardware", year_99:"5", year_00:"5", year_01:"5", year_02:"5", year_03:"5",
                                       year_04:"5", year_05:"5", year_06:"5", year_07:"5", year_08:"5", year_09:"5"},
                                      {trim:"Power Equip.", year_99:"5", year_00:"5", year_01:"5", year_02:"5", year_03:"5",
                                       year_04:"5", year_05:"5", year_06:"5", year_07:"5", year_08:"5", year_09:"5"},
                                      {trim:"Audio System", year_99:"5", year_00:"5", year_01:"5", year_02:"5", year_03:"5",
                                       year_04:"5", year_05:"5", year_06:"5", year_07:"5", year_08:"5", year_09:"5"}]},
                                      
                       {trim:"V6 4WD", year_99:"5", year_00:"5", year_01:"5", year_02:"5", year_03:"5",
                                        year_04:"5", year_05:"5", year_06:"5", year_07:"5", year_08:"5", year_09:"5", children: [
                                      {trim:"Engine Major", year_99:"5", year_00:"5", year_01:"5", year_02:"5", year_03:"5",
                                       year_04:"5", year_05:"5", year_06:"5", year_07:"5", year_08:"5", year_09:"5"},
                                      {trim:"Engine Minor", year_99:"5", year_00:"5", year_01:"5", year_02:"5", year_03:"5",
                                       year_04:"5", year_05:"5", year_06:"5", year_07:"5", year_08:"5", year_09:"5"},
                                      {trim:"Engine Cooling", year_99:"5", year_00:"5", year_01:"5", year_02:"5", year_03:"5",
                                       year_04:"5", year_05:"5", year_06:"5", year_07:"5", year_08:"5", year_09:"5"},
                                      {trim:"Transmission Major", year_99:"5", year_00:"5", year_01:"5", year_02:"5", year_03:"5",
                                       year_04:"5", year_05:"5", year_06:"5", year_07:"5", year_08:"5", year_09:"5"},
                                      {trim:"Transmission Minor", year_99:"5", year_00:"5", year_01:"5", year_02:"5", year_03:"5",
                                       year_04:"5", year_05:"5", year_06:"5", year_07:"5", year_08:"5", year_09:"5"},
                                      {trim:"Drive System", year_99:"5", year_00:"5", year_01:"5", year_02:"5", year_03:"5",
                                       year_04:"5", year_05:"5", year_06:"5", year_07:"5", year_08:"5", year_09:"5"},
                                      {trim:"Fuel System", year_99:"5", year_00:"5", year_01:"5", year_02:"5", year_03:"5",
                                       year_04:"5", year_05:"5", year_06:"5", year_07:"5", year_08:"5", year_09:"5"},
                                      {trim:"Electrical System", year_99:"5", year_00:"5", year_01:"5", year_02:"5", year_03:"5",
                                       year_04:"5", year_05:"5", year_06:"5", year_07:"5", year_08:"5", year_09:"5"},
                                      {trim:"Climate System", year_99:"5", year_00:"5", year_01:"5", year_02:"5", year_03:"5",
                                       year_04:"5", year_05:"5", year_06:"5", year_07:"5", year_08:"5", year_09:"5"},
                                      {trim:"Suspension", year_99:"5", year_00:"5", year_01:"5", year_02:"5", year_03:"5",
                                       year_04:"5", year_05:"5", year_06:"5", year_07:"5", year_08:"5", year_09:"5"},
                                      {trim:"Brakes", year_99:"5", year_00:"5", year_01:"5", year_02:"5", year_03:"5",
                                       year_04:"5", year_05:"5", year_06:"5", year_07:"5", year_08:"5", year_09:"5"},
                                      {trim:"Exhaust", year_99:"5", year_00:"5", year_01:"5", year_02:"5", year_03:"5",
                                       year_04:"5", year_05:"5", year_06:"5", year_07:"5", year_08:"5", year_09:"5"},
                                      {trim:"Paint/Trim", year_99:"5", year_00:"5", year_01:"5", year_02:"5", year_03:"5",
                                       year_04:"5", year_05:"5", year_06:"5", year_07:"5", year_08:"5", year_09:"5"},
                                      {trim:"Squeaks & Rattles", year_99:"5", year_00:"5", year_01:"5", year_02:"5", year_03:"5",
                                       year_04:"5", year_05:"5", year_06:"5", year_07:"5", year_08:"5", year_09:"5"},
                                      {trim:"Body Hardware", year_99:"5", year_00:"5", year_01:"5", year_02:"5", year_03:"5",
                                       year_04:"5", year_05:"5", year_06:"5", year_07:"5", year_08:"5", year_09:"5"},
                                      {trim:"Power Equip.", year_99:"5", year_00:"5", year_01:"5", year_02:"5", year_03:"5",
                                       year_04:"5", year_05:"5", year_06:"5", year_07:"5", year_08:"5", year_09:"5"},
                                      {trim:"Audio System", year_99:"5", year_00:"5", year_01:"5", year_02:"5", year_03:"5",
                                       year_04:"5", year_05:"5", year_06:"5", year_07:"5", year_08:"5", year_09:"5"}]}

       

                  ]);
                 
                  private function initListeners():void{
                      adg.addEventListener(AdvancedDataGridEvent.ITEM_OPEN, nodeOpenHandler);
                      adg.addEventListener(AdvancedDataGridEvent.ITEM_CLOSE, nodeCloseHandler);
                      adg.rowCount = dpHierarchy.length + 2;
                      //adg.expandAll();
                  }
                 
                  private function nodeOpenHandler(e:AdvancedDataGridEvent):void{
                      var rowClicked:int = dpHierarchy.getItemIndex(e.item);
                      if(rowClicked>0){
                          dpHierarchy.addItemAt({trim:"", year_99:"99", year_00:"00", year_01:"01", year_02:"02", year_03:"03",
                                                  year_04:"04", year_05:"05", year_06:"06", year_07:"07", year_08:"08", year_09:"09"}, rowClicked);
                              adg.coloredRowIndex = rowClicked;
                             adg.rowCount += 20;
                         }else if(rowClicked==0){
                             adg.rowCount += 19;
                         }

       


                  }
                 
                  private function nodeCloseHandler(e:AdvancedDataGridEvent):void{
                      var rowClicked:int = dpHierarchy.getItemIndex(e.item);
                      if(rowClicked>0){
                          dpHierarchy.removeItemAt(rowClicked-1);
                          adg.rowCount -= 16;
                      }else if(rowClicked==0){
                             adg.rowCount -= 15;
                         }
                  }
                
                 
                  private function rptHeaderStyle(data:Object, col:AdvancedDataGridColumn):Object{
                       if( data['trim'] == "" )
                       {
                           return {fontWeight:"bold", textAlign:"center"}
                       }
                       return {};
                  }

       

              ]]>
          </mx:Script>
         
          <comp:CustomRowColorDataGrid id="adg" width="75%" defaultLeafIcon="{null}" verticalScrollPolicy="off"
              sortExpertMode="true" sortableColumns="false" resizableColumns="false" draggableColumns="false"
              headerColors="[#ECF3FB, #F8F8F8]" selectable="false"
              folderClosedIcon="{null}" folderOpenIcon="{null}"
              disclosureClosedIcon="@Embed(source='assets/icon_close.gif')" disclosureOpenIcon="@Embed(source='assets/icon_close.gif')"
              alternatingItemColors="white" styleFunction="rptHeaderStyle">
              <comp:dataProvider>
                  <mx:HierarchicalData source="{dpHierarchy}"/>
              </comp:dataProvider>
              <comp:groupedColumns>

       

                  <mx:AdvancedDataGridColumnGroup headerText="Version">
                      <mx:AdvancedDataGridColumn dataField="trim" headerText="" width="165"/>
                  </mx:AdvancedDataGridColumnGroup>
                  <mx:AdvancedDataGridColumnGroup headerText="Used Car Verdicts">
                      <mx:AdvancedDataGridColumn dataField="year_99" headerText="99"/>
                      <mx:AdvancedDataGridColumn dataField="year_00" headerText="00"/>
                      <mx:AdvancedDataGridColumn dataField="year_01" headerText="01"/>
                      <mx:AdvancedDataGridColumn dataField="year_02" headerText="02"/>
                      <mx:AdvancedDataGridColumn dataField="year_03" headerText="03"/>
                      <mx:AdvancedDataGridColumn dataField="year_04" headerText="04"/>
                      <mx:AdvancedDataGridColumn dataField="year_05" headerText="05"/>
                      <mx:AdvancedDataGridColumn dataField="year_06" headerText="06"/>
                      <mx:AdvancedDataGridColumn dataField="year_07" headerText="07"/>
                      <mx:AdvancedDataGridColumn dataField="year_08" headerText="08"/>
                  </mx:AdvancedDataGridColumnGroup>
                  <mx:AdvancedDataGridColumnGroup headerText="New Car Prediction">
                      <mx:AdvancedDataGridColumn dataField="year_09" headerText="09" width="130"/>
                  </mx:AdvancedDataGridColumnGroup>

       

              </comp:groupedColumns>

       

          </comp:CustomRowColorDataGrid>

       


      </mx:Application>