views:

1648

answers:

1

Hi,

I have a quick question about rendering the advanceddatagrid cells.

I need to programatically color the cell of the datagrid based on the conditions. Lets say, the stock quotes. If there is an increase from the previous day, I need to have the cell colored in GREEN and in RED, when there is a decrease.

Now, the important part here is, I need to do these things dynamically, which means, when the user enables the comparison/conditions, then the cells are colored. And when the user disables the comparison, then it again goes back to its default behavior.

I know I have to use renderers. But not sure, how to use it for the cells and that too dynamically. Can anyone please explain how to go for it?

Thanks

+2  A: 

Item renderers are components used to define the appearance of a component's "items" or subcomponents. In the case of the ADG, the "items" are the individual cells. You can create a completely custom class to function as the renderer (given it implements certain required interfaces) or, in most cases, you extend an existing component. Since the default renderer for ADG cells doesn't support background colors, you have to create or extend a component that does and use that as the renderer. That is the basic premise that these tutorials, linked to in the following question, work from:

http://stackoverflow.com/questions/748213/setting-background-color-for-datagrid-row-in-adobe-flex/748342#748342

After creating an itemRenderer that supports a background color, you have two options as to where you can define your "conditions"; inside of the itemRenderer or using the ADG's styleFunction (additionally requiring that your itemRenderer defines a "background" style).

In your case, you could include both today's and yesterday's stock price values in the data sent to each cell and compare the two to determine the color used to draw the background. Again, more on that in the tutorial links provided above. In either the itemRenderer or the styleFunction, you would compare properties on the itemRenderer's/styleFunction's data object (corresponding to the row you're looking at), e.g.:

if(data.today > data.yesterday)
{
    // set color or return style
}
else ...

To "toggle" custom cell colors, switch between your custom renderer and the default (colorless) renderer. In other words, set the itemRenderer property to your custom itemRenderer class when you need display the colors and set it to "null" when you want the "default behavior".

Stiggler
Thanks for your reply. I could use ITEMRENDERER for one of my columns where I have to display RED/GREEN if the value is above/below 0. But I am still finding hard to compare the cell with the other cells in the same row. As in, I have 2 columns TODAY, YESTERDAY. I need to paint TODAY based on the values from TODAY and YESTERDAY. Which means, if for one row TODAY > YESTERDAY, TODAY will be GREEN otherwise it will be RED.
online19
Though you can make comparisons between columns inside the itemRenderer, it would probably be a lot easier for you to take care of it prior to assigning a dataProvider to the ADG. For example, you could create a new property on the data object in the dataProvider, perhaps a "higherThanYesterday" boolean value. Calculate it out and then just color according to the value of that property.
Stiggler
I am not allowed to change the query though. You mentioned that it is possible to make comparisons between the columns inside itemRenderer. Can you please point me to any resource where I can get to know more about it?
online19
What does your dataProvider look like?
Stiggler
it is an arraycollection, which is populated from the query to DB.
online19
Assuming your ArrayCollection contains objects with properties like "today" and "yesterday" (to which your ADGColumns dataFields corresponds), you can easily compare the values of data.today and data.yesterday in your itemRenderer.
Stiggler
Awesome! I could do that! Thanks a lot for your responses. I have done the similar thing in my other columns using StyleFunction with less efforts :)
online19