Link to home
Start Free TrialLog in
Avatar of Jay Roy
Jay RoyFlag for United States of America

asked on

if-else condition in mxml

hi guys

I need to impliment an If - else  condition in my mxml file to show and hide data.

The requirment is if 'username' is jay

i need to show  <mx:Button  label="Create new User" />

else if username is Marco i need to show <mx:Button  label="Create new Admin" />

Are there any <mx: tags using which i can do that?

right now i have this

                  <mx:Script>
                  private function userHandler(event:ResultEvent):void
                  {
                        user = event.result;
                        username = user.name

                  }                  
                  ]]>
                  </mx:Script>


                  <mx:Form width="1094" height="2200">      
                  <mx:HBox width="100%"  >                        
                        <mx:Button  label="Create new User" />      if username is jay show this , else hide it                                      
                  </mx:HBox>
                  
                  <mx:HBox width="100%"  >                        
                        <mx:Button  label="Create new Admin" />      if username is marco show this , else hide it                               
                  </mx:HBox>
                         
</mx:Form>

thanks for any help
Avatar of petiex
petiex
Flag of United States of America image

This should work:

                  <mx:Script>
                  [Bindable]
                  private var username:String;
                  private function userHandler(event:ResultEvent):void
                  {
                        user = event.result;
                        username = user.name

                  }                  
                  ]]>
                  </mx:Script>


  <mx:Form width="1094" height="2200">      
       <mx:HBox width="100%"  >                        
          <mx:Button  label="Create new User" visible="{username == 'jay'}"/>      
          <mx:Button  label="Create new Admin" visible="{username == 'macro'}"/> 
        </mx:HBox>
                         
</mx:Form>

Open in new window

Avatar of Jay Roy

ASKER

thanks for the response

>>visible="{username == 'jay'}"/

I dont want to use the visible attribute because it creates empty space. Here is my exact code which uses visible:


<mx:Form width="1094" height="2200">      
<mx:HBox width="100%" horizontalAlign="center"  direction="horizontal"
    paddingLeft="10" paddingRight="10" visible="{username == 'jay'}" >            
<projectNav:MagnifyingButton label="New "/>
<projectNav:MagnifyingButton label="Delete"/>
<projectNav:MagnifyingButton label="Copy "/>
<projectNav:MagnifyingButton label="Send to LEAD"/>
</mx:HBox>
                  
<mx:HBox width="100%" horizontalAlign="center" direction="horizontal"  
          paddingLeft="10" paddingRight="10" visible="{username == 'marco'}" >      
            <projectNav:MagnifyingButton label="Approve"/>
            <projectNav:MagnifyingButton label="Reject"/>
</mx:HBox>
</mx:form>

now whenever user marco logs in there will be a big blank space caused by the First <mx:HBOX> .
That is why i dint want to use visible. Is there anyway we avoid that whitespace ?

thanks


Yeah, just make the width contingent on the user name as well. There's a little trick here where the NaN (not-a-number) expression lets the component have its default width. You can do the same with height if necessary:

      <mx:HBox width="100%"  >                        
          <mx:Button  label="Create new User" width="{username == 'jay'?NaN:0}" visible="{username == 'jay'}"/>      
          <mx:Button  label="Create new Admin" width="{username == 'macro'?NaN:0}" visible="{username == 'macro'}"/>
        </mx:HBox>
Sorry, I missed your updated code there. You could do this or use states.

<mx:HBox width="100%" horizontalAlign="center"  direction="horizontal"
    paddingLeft="10" paddingRight="10" visible="{username == 'jay'}" height="{username == 'jay'?NaN:0}" >            
    <projectNav:MagnifyingButton label="New "/>
    <projectNav:MagnifyingButton label="Delete"/>
    <projectNav:MagnifyingButton label="Copy "/>
    <projectNav:MagnifyingButton label="Send to LEAD"/>
</mx:HBox>
                  
<mx:HBox width="100%" horizontalAlign="center" direction="horizontal"  
          paddingLeft="10" paddingRight="10" visible="{username == 'marco'}"  height="{username == 'jay'?NaN:0}">      
            <projectNav:MagnifyingButton label="Approve"/>
            <projectNav:MagnifyingButton label="Reject"/>
</mx:HBox>

Open in new window

Avatar of Jay Roy

ASKER

ok cool thanks
one question
>>height="{username == 'jay'?NaN:0}"
User is not jay height is zero and if user is Jay what is the height ?
ASKER CERTIFIED SOLUTION
Avatar of petiex
petiex
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Jay Roy

ASKER

thanks, that helps. Can you please help me with my next question...
https://www.experts-exchange.com/questions/26865398/removing-redundancy-in-mxml.html