PDA

View Full Version : DataGridColumn itemRenderer, possible like this?


michael_teter
04-10-2008, 02:46 AM
Howdy.

I'm very new to Flex (3), and I've got this DataGrid. I want to programmatically affect the contents of some of the columns based on their underlying data. I know this is possible, and I've seen some examples of how to do it. However, I haven't seen one quite like how I envision it (and hope to be able to do it).

Here's my expectation of how it should be possible. If it is possible this way, then I need guidance on syntax or declarations/arguments to get it right.

My pseudocode idea:

<mx:Script>
<![CDATA[
private function myRenderer(???) : ? {
if (currentCellData == "Dog") ?make the text presented in the cell be "D"?
else ?make the text presented in the cell be "-"?
}
]]>
</mx:Script>

...
<mx:DataGrid ...>
<mx:DataGridColumn ... itemRenderer="{myRenderer}"/>

That's about as simple as I can describe it. I've seen some interesting examples here and there, including http://weblogs.macromedia.com/pent/ , but so far I don't believe I've seen something exactly like I describe.

Lastly, in the body of the function myRenderer(), I will also later want to instead show an image instead of text. I assume I would be creating an image control of some sort and returning that to the cell.

Thanks very much.
Michael

kahuja
04-10-2008, 03:46 AM
If possible. You will have to write your own Item Renderer class that then will handle the rendering and you can have conditional rendering as well, based on the data that teh Item renderer receives.

A nice link that I was using a while back should give you a heads start (you will have to do the rest)

http://blog.paranoidferret.com/index.php/2007/08/29/flex-fun-advanced-datagrid-topics/

michael_teter
04-12-2008, 05:30 AM
Thanks for the good link.

For the benefit of anyone else hoping to find the exact answer to this (as I consider it to be fitting my needs), here's how I accomplished this. The thing I was looking for (in this simplistic, String case) is called labelFunction.

(script section)
import mx.controls.dataGridClasses.DataGridColumn;

private function renderAlert(item:Object, col:DataGridColumn):String {
if (item[col.dataField] == "Critical") return "!";
else if (item[col.dataField] == "Warning") return "w";
else return "?";
}
(mxml section)
<mx:DataGrid ...>
<mx:DataGridColumn dataField="alert" labelFunction="renderAlert"/>
...
</mx:DataGrid>