Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flex - Sending a parameter to a custom ItemRenderer?

Tags:

What I am trying to accomplish to to get financial data in my Flex Datagrid to be color-coded--green if it's positive; red if it's negative. This would be fairly straightforward if the column I want colored was part of the dataProvider. Instead, I am calculating it based on two other columns that are part of the dataProvider. That would still be fairly straightforward because I could just calculate it again in the ItemRenderer, but another part of the calculation is based on the value of a textBox. So, what I think I need to be able to do is send the value of the textBox to the custom ItemRenderer, but since that value is stored in the main MXML Application, I don't know how to access it. Sending it as a parameter seems like the best way, but perhaps there's another.

Here is the current code for my ItemRenderer:

package { import mx.controls.Label; import mx.controls.listClasses.*;  public class PriceLabel extends Label {     private const POSITIVE_COLOR:uint = 0x458B00 // Green     private const NEGATIVE_COLOR:uint = 0xFF0000; // Red       override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void {         super.updateDisplayList(unscaledWidth, unscaledHeight);          /* Set the font color based on the item price. */         setStyle("color", (data.AvailableFunding >= 0) ? NEGATIVE_COLOR : POSITIVE_COLOR);     } } 

(data.AvailableFunding doesn't exist)

So does anyone know how I would go about accomplishing this?

like image 387
Matt Dell Avatar asked Feb 09 '09 16:02

Matt Dell


2 Answers

You may want to look into ClassFactory from the Flex APIs:

This allows you to set a prototype Object with arbitrary types / values each of which will be passed to the item renderer. From the sample:

var productRenderer:ClassFactory = new ClassFactory(ProductRenderer); productRenderer.properties = { showProductImage: true }; myList.itemRenderer = productRenderer; 

The above code assumed that "ProductRenderer" has a public property called "showProductImage" which will be set with a value of "true."

like image 197
cliff.meyers Avatar answered Oct 18 '22 11:10

cliff.meyers


Ah, so I knew about outerDocument but not parentDocument. I was able to just use parentDocument.*whatever I want from the main App and I can access it as long as it's public.

Example:

setStyle("color", (parentDocument.availableFunding >= 0) ? POSITIVE_COLOR : NEGATIVE_COLOR); 

Sweet! :)

like image 36
Matt Dell Avatar answered Oct 18 '22 09:10

Matt Dell