7 Replies Latest reply on Apr 6, 2010 10:28 AM by Flex harUI

    DataGrid ItemRenderer First Row

    play();

      Hi,

       

      I have a DataGrid:

       

      <mx:DataGrid  id="gamesGrid" x="10" y="81" width="828" variableRowHeight="false">
              <mx:columns>
                  <mx:DataGridColumn headerText="Game" itemRenderer="GameContainer"/>
                  <mx:DataGridColumn headerText="Starts"  itemRenderer="StartsContainer"/>
              </mx:columns>
      </mx:DataGrid>

       

      Its dataProvider is an XMLListCollection.

       

      Its ItemRenderer are VBox objects (either written in Actionscript or in MXML - either way).

       

      My problem is, that when the XMLListCollection is updated, my ItemRenderer's are either reinstantiated or reinitialised.

       

      However, the creationComplete handler of the ItemRenderer (in my case a VBox) is only called for the first row of the DataGrid.

       

      This is a problem, because it clears the display data.

       

      If i have the ItemRenderer in code, then i see the constructor called when i update the dataProvider.

       

      If i have ItemRenderera an MXML component, then i see creationComplete called when i update the dataProvider.

       

      All i want to do is update the contents of the ItemRenderer - not re instantiate it (especially not JUST the first row of ym datagrid).

       

      How do i get around this and is this a bug?

       

      Sounds quite similar to:

       

      http://forums.adobe.com/thread/604259

       

      Regards, Sam

        • 1. Re: DataGrid ItemRenderer First Row
          dave cragg Level 2

          Apologies if I've misunderstood your problem.

           

          But if you just want to update the data displayed in the renderer when the dataProvider data changes, it's normal to override the "set data" function in the renderer. Simple example based on those found in the docs:

           

          <mx:VBox xmlns:mx="http://www.adobe.com/2006/mxml"

              horizontalAlign="center">

           

              <mx:Script>

                  <![CDATA[

           

                      import mx.events.FlexEvent;

           

           

                      override public function set data(value:Object):void {     

                          if(value != null)  {

                              super.data = value;

           

                              // do whatever you need to do here

                              // for example

                              var xml:XML = value as XML;

                              myTextArea.text = xml.myProperty

                          }  

                          // Dispatch the dataChange event.

                          dispatchEvent(new FlexEvent(FlexEvent.DATA_CHANGE));

                      }  

                  ]]>

              </mx:Script>

           

              <mx:TextArea id="myTextArea"/>

          </mx:VBox>

          As renderer instances are recycled in data grids, I think using creationComplete will have unpredictable results.

          • 2. Re: DataGrid ItemRenderer First Row
            Flex harUI Adobe Employee

            Renderers are recycled so creationComplete only gets called once.  See the

            oldest posts in the item renderers category on my blog for more details

             

             

            --

            Alex Harui

            Flex SDK Team

            Adobe System, Inc.

            http://blogs.adobe.com/aharui

            • 3. Re: DataGrid ItemRenderer First Row
              play(); Level 1

              Hi dave cragg

              dave cragg wrote:

               

              if you just want to update the data displayed in the renderer

               

               

              No - thats not just what i am trying to do.  My problem is that the whole ItemRenderer is re-instantiated and reinitialised.  I dont have a problem with data.

              • 4. Re: DataGrid ItemRenderer First Row
                play(); Level 1

                Hi Flex harUI,

                 

                Flex harUI wrote:

                 

                so creationComplete only gets called once.

                 

                .. no it doesnt (well, not in my case anyway).

                 

                Let me be clear:

                 

                When i update the dataProvider or my dataGrid - the ItemRenderers in the first row of my dataGrid are:

                 

                Reinstantiated (if written in code).

                Reinitilaised / creationComplete (if written in MXML).

                 

                So the display is cleared and the data is lost.

                 

                Regards, Sam

                • 5. Re: DataGrid ItemRenderer First Row
                  Flex harUI Adobe Employee

                  I would think that if you don't use creationComplete and make sure all

                  visuals are driven from the data property it wouldn't matter if renderers

                  get tossed and recreated or recycled.  Do not rely on creationComplete when

                  coding renderers.

                  • 6. Re: DataGrid ItemRenderer First Row
                    play(); Level 1

                    Hi,

                     

                    Thanks for your help.

                     

                    Flex harUI wrote:

                     

                    it wouldn't matter if renderers

                    get tossed and recreated or recycled.

                     

                    I don't see how it would not matter.  If a constructor is called (re-called) or the item renderer is recreated, then thats going to get rid of the information i am displaying.

                     

                    Also - it only happens on the first row... this cannot be normal behaviour.

                     

                    Regards, Sam

                    • 7. Re: DataGrid ItemRenderer First Row
                      Flex harUI Adobe Employee

                      If all visuals are driven from the data and listData properties, it will not

                      matter.  Please read about recycling of renderers.  We do not guarantee that

                      a renderer will stay around forever or that it will get used to show the

                      same data item.