Solved

if-else condition in mxml

Posted on 2011-03-04
7
2,465 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 11

Expert Comment

by:petiex
ID: 35038994
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
ID: 35039783
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
ID: 35039954
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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
LVL 11

Expert Comment

by:petiex
ID: 35040020
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
ID: 35040354
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
ID: 35041373
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
ID: 35044007
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

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to Develop Photo Share Android App 6 498
How popular is Java FX these days? 5 366
AS3 parent function 3 679
Flex/Air App seeing online status of client from an admin user 1 404
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…
Building a cohesive image for your brand is vital to making an impression on consumers. When the economy is tough, brands do better than unbranded  products. This can have a huge impact on your long-term profits, as the economy goes up and down.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

763 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