Solved

if-else condition in mxml

Posted on 2011-03-04
7
2,349 Views
Last Modified: 2012-05-11
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
0
Comment
Question by:royjayd
  • 4
  • 3
7 Comments
 
LVL 11

Expert Comment

by:petiex
Comment Utility
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

0
 

Author Comment

by:royjayd
Comment Utility
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


0
 
LVL 11

Expert Comment

by:petiex
Comment Utility
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>
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 11

Expert Comment

by:petiex
Comment Utility
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

0
 

Author Comment

by:royjayd
Comment Utility
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 ?
0
 
LVL 11

Accepted Solution

by:
petiex earned 500 total points
Comment Utility
Well, when you say height="NaN", it is sort of like saying "forget that I ever set the height at all." For an HBox, that means set the height big enough to contain its child components.
0
 

Author Comment

by:royjayd
Comment Utility
thanks, that helps. Can you please help me with my next question...
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/Flex/Q_26865398.html
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

First things first - Preparation We need all the part for this install and it's much nicer to have them all on hand when you need them so here's what's required. Download Eclipse 3.5 32 bit (I like the Classic flavour) from here. (http://www.e…
Find out what Office 365 Transport Rules are, how they work and their limitations managing Office 365 signatures.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now