8 Replies Latest reply on Dec 22, 2006 12:57 PM by xyco45

    communicating with components in the ItemRenderer

    xyco45
      I have a label component inside the itemRenderer of the datagrid I created but, I want to change the text and color of the label but for some reason I can't give the label component a id; I get this error "id attribute is not allowed on the roo tag of a component" how am I suppose to communicate with with it if I can't even reference it...
        • 1. Re: communicating with components in the ItemRenderer
          peterent Level 2
          You are supposed to communicate with an itemRenderer via the data it is given. Why do you want to change its color? Is it because of a value in the data record? For example, if the itemRenderer's text should be red when a value is less than 0 and green otherwise you should do:

          override protected function updateDisplayList( unscaledWidth:Number, unscaledHeight:Number ) : void
          {
          super.updateDisplayList(unscaledWidth,unscaledHeight);
          if( data.value < 0 ) label.setStyle("color", 0xFF0000);
          else label.setStyle("color",0x00FF00);
          }

          itemRenderer instances are difficult to reach because they are re-used and you never really know for which record any particular instance will be used. Example: you have 100 records to display. The DataGrid shows 10 records, so there are only 10 itemRenderers created per column. You scroll up 1 record, bringing the 11th row into view. The itemRenderers used for rows 2-10 are shifted upward. The itemRenderer for row 1 is moved to the bottom and is given the value from the 11th row. Scroll again and 3-11 are shifted upward and the itemRenderer for row 2 is moved to the bottom and given the value from row 12.

          There's no guarantee that will be the mechanism for the next release of Flex, either. We want you to use the data to determine the itemRenderer's actions.
          • 2. Re: communicating with components in the ItemRenderer
            xyco45 Level 1
            Yes correct, that is pretty much what I am trying to do I want a way so I can control the labels text color in each row based on some sort of condition. In my case what I wanted to do is set it up so that the data provider supplies the datagrid with three objects, two of them with a string value and the last with a RGB value. Using a Label function I wanted to some how create a method call or some other action that will change the color of the text in the label based on the value in the current row index of the rgb value. Now the way you supplied me seems like it could work, but when I placed the overrided function that you supplied me inbetween the <script> tags it gave me this error " call to a possibly undefined method setStyel through a reference with a static type String. " I know what the "data.value" represents, but what does the "label" represent? It seems to be a string value.. but where did it come from? do I need to import something to make it all work? if you could explained the code you gave me a bit more it would be greatly appreciated! Because I am pretty sure the code is doing excatly what I want.
            • 3. Re: communicating with components in the ItemRenderer
              peterent Level 2
              You could not use the labelFunction to change the color - all it does it provide a string for display.

              As for the 'label' reference, I was assuming you had a <mx:Label id="label" ... /> or <mx:Text id="label" ... /> in your itemRenderer class.
              • 4. communicating with components in the ItemRenderer
                xyco45 Level 1
                Oh thanks for telling me about the Label function.. didn't know that, uhm.. soo I need to create a item renderer class then? I wasn' t doing it like that.. it was "inline"

                <mx:DataGridColumn headerText="name" dataField="name">
                <mx:itemRenderer >
                <mx:Component>
                <mx:Label text = "fsfd" />
                </mx:Component>
                </mx:itemRenderer>
                </mx:DataGridColumn>
                I would have assigned a id but as I said before I kept getting that error about not being able to assign a id in the root component, etc, etc. do I need to make a custom item renderer and like set it up like this.. <datagrid ItemRenderer = "customRenderer" > ..... how would I set it up? please try and bare with me on this a little new when it comes to dealing with itemRenderer and THANX a bunch for your help.. been stuck on this problem for a while.
                • 5. Re: communicating with components in the ItemRenderer
                  peterent Level 2
                  You could try this:
                  <mx:itemRenderer>
                  <mx:Component>
                  <mx:Canvas >
                  <mx:Script><![CDATA[
                  override public function set data( value:Object ):void
                  {
                  super.data = value;
                  setStyle("backgroundColor", value.rgb);
                  }
                  ]]>
                  </mx:Script>
                  <mx:Label text="{data.name}" /> <!-- or whatever field from your record should go here -->
                  </mx:Canvas>
                  </mx:Component>
                  • 6. Re: communicating with components in the ItemRenderer
                    xyco45 Level 1
                    I changed "background" to "color" and it worked, YES, YES, YES, THANK YOU, THANK, THANK YOU!!!!!! :-D one last thing I have to ask of you... you know any good places that explain the whole overridding methods thing? because it clearly seems to be very handy, once again THANX A MILLION!!!
                    • 7. Re: communicating with components in the ItemRenderer
                      peterent Level 2
                      As you've probably noticed, using Flex and ActionScript assume a level of knowledge of programming and programming practices. So looking at the Adobe Flex documentation won't help you in this case.

                      Someone is supposed to be publishing several Flex 2 books, one of which (at least) is about ActionScript 3.0 and should cover overriding methods. Check Amazon.com to see if they have it.
                      • 8. Re: communicating with components in the ItemRenderer
                        xyco45 Level 1
                        Yah I am reading the Flex cookbook right now, lol.. figured out how to do the overriding thing, its pretty nice and so is teh display objects setup, was very nice seeing that it has movie clips and what not, coming from flash that made me feel very at home :-) same with the whole creating objects and what not, it all seems so very familiar to java (makes sense being tht it is oo language) which is now making tings go very smoothly, thanks again for the help :-)