2 Replies Latest reply on Feb 15, 2011 3:45 AM by CloudNine99

    Custom Itemrenderers


      Hi Guys,

                  I have written a small example where i have a datagrid populated with random numbers and if the numbers are positive the text colour is green, otherwise red (based on using a label as a custom renderer). This is all fine up to the point where i want to change the background of the label to green if the value is incremented and red if the value is decremented (using the Flex timer). Ive tried to create a variable called "oldValue" on the custom label class to store the original value so that i can use it as part of the condition to determine the background colour. But as itemrenderers are reusuable it seems that the old value does not refer to the same instance as it was created with. Does anyone know a solution for this?



        • 1. Re: Custom Itemrenderers
          UbuntuPenguin Level 4

          Do you have a model of some kind ?  If you don't I would have something like this



          public class StockQuote extends EventDispatcher implements IUID


          public var currentPrice:Number;

          pubilc var previousPrice:Number;



          In your itemrenderer I would do something like this.


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


            super.data = value;

            if( value != null )


               var stockQuote:StockQuote = value as StockQuote;

               if( stockQuote.currentPrice > stockQuote.previousPrice )


                    //run function to make background green


                else if( stockQuote.currentPrice < stockQuote.previousPrice  )


                    //run function to make background red





          Don't put a custom property on the label because whether or not the price has changed belongs more in the model. That way, it doesn't matter how the prices and renderers are used/reused, the data used to calculate what color it should be stays in sync with itself.  You may have to add a listener to the stock quote and do some other stuff to get it to work correctly on price changes.

          1 person found this helpful
          • 2. Re: Custom Itemrenderers
            CloudNine99 Level 1

            Thank you for your answer Ubuntu, where would be the correct place to set the previous price? Im trying to set it in the creationComplete method for itemRenderer but it still seems to mismatch the different values from other renderers. For example if i have 2 cells in one column, lets say 0 and 7. If i increment both by one and then Alert out the previous and current values of stockQuote i get the following.


            • oldquote: 0 new quote: 8
            • oldquote: 7 new quote: 1  


            Im guessing this is because of the recycling of the itemrenderers?