Thursday, May 15, 2008

Flex 3 - Get the text of your DataGrid Item

The task (which you would really think should be simple) is to get the text of the current DataGrid element in a custom drop-in itemRenderer. This can be useful for a lot of reason - for me specifically, I wanted to use the same renderer for an entire DataGrid and color things one color if they're less than 5 and another if they are more than 5. But to do that, you have to create an itemRenderer that knows which column it is in in order to retreive the correct data. Sadly, this took me several hours of searching and not only could I not find any proper solutions, but I ran across a number of dated examples that sent me in the wrong direction.



So, without further ado, getting the text of your DataGrid item in an ItemRenderer:


private function getItemText():Object
{
return data.elements(getColumn().headerText);
}

private function getColumn():DataGridColumn
{
var dg:DataGrid = (listData) ? DataGrid(listData.owner) : null;
var column:DataGridColumn = (dg) ? dg.columns[listData.columnIndex] as DataGridColumn : null;

return column;
}

5 comments:

Unknown said...

YOU BLOGGED!!!! :) :*

MaryT said...

Sorry, I'm enough of a newbie that I'm not able to quite follow the code. I'm not sure what some of the variables represent. what is the "listData" and the "listData.owner"? Also, my grid has 3 columns; I want to change the image in one column, but I might have clicked on any of the columns so I would be using a hard coded "columnIndex".

Tim "Palantar" Jones said...

Good news Betty, your code would then be significantly simpler :) If you know which column to access, then you won't need any of this code, just hard code the columnIndex and set your image component to a new image.

In this code, the listData is an inherited variable which by default contains the data stored in the DataGrid. Unfortunately, I don't have my Flex code around right now so I can't remember how exactly the listData gets inherited, but I believe it is available to every Renderer.

For listData.owner, owner is a member variable of listData which includes a reference back to the object that created it. It can be a different type in difference circumstances, but for a datagrid, the owner will be of type DataGrid.

Hope that helps!

kumar said...

I have a TextArea as the CellRenderer for one of the GridColumns. How do I access the TextArea control of that grid column?

Thanks,
-Kumar.

Tim "Palantar" Jones said...

Happyhop: What are you trying to do? Your question is a little tricky since I am not sure where your are attempting to access your CellRenderer from and for what purpose. Each different (visible) ItemRenderer is also going to be a different instance as well. Typically, if you would want an individual ItemRenderer to do something special you would do so by setting the ItemRenderer up itself. So, in this case, I suspect that what you are trying to do might best be done by deriving from the TextArea class to form your own custom itemRenderer.