Flex / Flash Builder – Inline Conditional, If Then, Code
Posted on : 02-11-2009 | By : Michael Fitchett | In : ActionScript, Flash Builder, Flex
0
Inline conditionals, coding, if then’s whatever you want to call it is soooo easy yet has been a pain in the royal _ _ _ for me to find maybe its the search terms I have been Google’ing I don’t know.
Here is a very simple example of how to use an inline conditional to show/hide a button if a check box is selected (checked).
The button has a visible property with the value set to a conditional statement that says if the check box is selected then show the button if it is not then hide the button by setting the value true or false.
visible="{(cbShowButton.selected == true) ? true : false}"
If this is your first time looking at an inline condition here is an image I have put together to explain in a little more detail and help you digest it a little easier

Flex / Flash Builder Inline Condition Example
The “?” is what ends the condition and the “:” acts as an else.
Yes, there are several ways of showing and hiding a button based on another components value. However I find this way to work out really well because you dont need to preset the button’s visible property to false before running the application. It detects that the check box is not selected at run time and hides the button. Of course you could just pass the check box selected value which is true or false anways but some or most cases you wont be checking againts a true or false value.
Inline conditions really shine when used with data grids. For example so you have a list of people attending an event and you want to show male or female by an icon you can easily accomplish this using an inline condition to check and see if the datagrid column value is male or female and set the image source to to the appropriate path.
<mx:Image source="{(data.colGender == 'male') ? 'assets/male.png' : 'assets/female.png'}"/>
We can also add to this condition by using a comma for example:
<mx:Image source="{(data.colGender == 'male', data.colAge > 21) ? 'assets/male.png' : 'assets/female.png'}"/>
Download Flex inline conditional example project here: Flex Inline Condition - Example Project (43)










