flex images: visible property funkiness in itemRenderer
may 13, 2008 at 9:56pm
in flex
in the flex app that i'm currently working on, i have a DataGrid that uses an itemRenderer to display an image. the image is a 'delete' button that allows the user to delete the item. fairly straightforward, and the code below was working fine...
- <mx:DataGridColumn width="24" sortable="false" paddingLeft="4" paddingRight="4" headerText="">
- <mx:itemRenderer>
- <mx:Component>
- <mx:Image source="assets/icon_delete.png" horizontalAlign="center" height="17" width="17" click="parentDocument.confirmDelete(event);" />
- </mx:Component>
- </mx:itemRenderer>
- </mx:DataGridColumn>
... until the customer called and said that the user should not be allowed to delete a record if that record meets a particular condition. ok. i think i can do that. i made some modifications and now had the following code:
- <mx:DataGridColumn width="24" sortable="false" paddingLeft="4" paddingRight="4" headerText="" dataField="CAN_DELETE">
- <mx:itemRenderer>
- <mx:Component>
- <mx:Image source="assets/icon_delete.png" horizontalAlign="center" height="17" width="17" click="parentDocument.confirmDelete(event);" visible="{data.CAN_DELETE == 1}" />
- </mx:Component>
- </mx:itemRenderer>
- </mx:DataGridColumn>
i added a "dataField" property to the DataGridColumn, and added a "visible" property to the image itself, bound to the value of CAN_DELETE. recompiled, and still saw that every record displayed the delete image. i wondered if my logic, as simple as it was, might have been off. just for kicks and giggles, i hard coded the value 'false' into the "visible" property of the image. still, it displayed for all records. now this was odd.
one of the big perks of my current job is that i get to work with tariq ahmed, flex guru and all around good guy. while he wasn't able to offer up a reason as to why this behavior was happening, he suggested throwing the image into an <mx:HBox> component. new code:
- <mx:DataGridColumn width="24" sortable="false" paddingLeft="4" paddingRight="4" headerText="" dataField="CAN_DELETE">
- <mx:itemRenderer>
- <mx:Component>
- <mx:HBox>
- <mx:Image source="assets/icon_delete.png" horizontalAlign="center" height="17" width="17" click="parentDocument.confirmDelete(event);" visible="{data.CAN_DELETE == 1}" />
- </mx:HBox>
- </mx:Component>
- </mx:itemRenderer>
- </mx:DataGridColumn>
you'll notice the only change is the addition of the <mx:HBox> around the <mx:Image>. for whatever reason, that worked perfectly. i don't pretend to know why, but i'm curious as heck. if you've got any ideas, please feel free to comment and enlighten me.
# May 14, 2008 @ 1:05 AM
This is means that your image was the item renderer, and the item renderer itself cannot be invisible. The item renderer can be blank, but not invisible. In your second case, the HBox is the item renderer, so the child component can change freely.
There are a few other weird cases which I have come across when using inline item renderers, so as a general rule, I always create item renderers as a seperate class. You then have to replace references to parentDocument with other solutions: http://www.munkiihouse.com/?p=47
# May 14, 2008 @ 1:46 AM