Solved

setting default selected value of a ComboBox?

Posted on 2008-06-17
17
2,895 Views
Last Modified: 2012-01-22
Is there a way to set the default selected Value of a combo box based on if a string matches the labelField Value?

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/01/16/determining-which-item-was-clicked-in-a-flex-linkbar-control/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white">
    <mx:Script>
        <![CDATA[
            import mx.controls.Alert;
            
            
 
                        var dataStr:String;
                        var animalStr:String;
            private function onClick1(eventtester:String):void {
                dataStr=eventtester;
                Alert.show('combo box should now change to ' + eventtester);
                animalsComboBox.selectedLabel == eventtester;
            }
            
            private function onClick2(eventtester2:String):void {
                Alert.show(eventtester2);
            }
            
 
        ]]>
    </mx:Script>
 
    <mx:Array id="animals">
        <mx:Object label="One" data="1" custom="cougar" />
        <mx:Object label="Two" data="2" custom="rhino" />
        <mx:Object label="Three" data="3" custom="elephant" />
        <mx:Object label="Four" data="4" custom="penguin" />
    </mx:Array>
    
 
    
<mx:Repeater dataProvider="{animals}" id="RP">
<mx:TextInput id="textBox" text="{RP.currentItem.custom}"/>     
 
 
 
<mx:Button id="myButton" label="{String(RP.currentItem.custom)}" click="onClick1(event.currentTarget.getRepeaterItem().custom)"/>
 
</mx:Repeater>
 
 
 
<mx:LinkButton id="myLinkbutton" label="Click Event 2" click="onClick2(this.dataStr)"/> 
	<mx:ComboBox id="animalsComboBox" dataProvider="{animals}" labelField="custom"></mx:ComboBox>
 
 
</mx:Application>

Open in new window

0
Comment
Question by:_CODER_
[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
17 Comments
 
LVL 23

Expert Comment

by:Siva Prasanna Kumar
ID: 21807222
What you want the default value as??

you can do it in two ways via  selectedItem or selectedIndex of the combo box.

in you case if you try something like selectedItem="3" for the comboBox
0
 

Author Comment

by:_CODER_
ID: 21807261
I want the selected item in the combobox to be the onClick1 value.
0
 

Author Comment

by:_CODER_
ID: 21807386
so if you click 'elephant' button the selected item in the combo box will be 'elephant'

0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
LVL 23

Expert Comment

by:Siva Prasanna Kumar
ID: 21807390
Just to get your code working I made few changes to the previous methods
<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/01/16/determining-which-item-was-clicked-in-a-flex-linkbar-control/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white">
    <mx:Script>
        <![CDATA[
            import mx.controls.Alert;
            
            
 
                        var dataStr:String;
                        var animalStr:String;
            private function onClick1(eventtester:Object):void {
                dataStr=eventtester.custom;
                Alert.show('combo box should now change to ' + eventtester);
                animalsComboBox.selectedItem = eventtester;
            }
            
            private function onClick2(eventtester2:String):void {
                Alert.show(eventtester2);
            }
            
 
        ]]>
    </mx:Script>
 
    <mx:Array id="animals">
        <mx:Object label="One" data="1" custom="cougar" />
        <mx:Object label="Two" data="2" custom="rhino" />
        <mx:Object label="Three" data="3" custom="elephant" />
        <mx:Object label="Four" data="4" custom="penguin" />
    </mx:Array>
    
 
    
<mx:Repeater dataProvider="{animals}" id="RP">
<mx:TextInput id="textBox" text="{RP.currentItem.custom}"/>     
 
 
 
<mx:Button id="myButton" label="{String(RP.currentItem.custom)}" click="onClick1(event.currentTarget.getRepeaterItem())"/>
 
</mx:Repeater>
 
 
 
<mx:LinkButton id="myLinkbutton" label="Click Event 2" click="onClick2(this.dataStr)"/> 
        <mx:ComboBox id="animalsComboBox" dataProvider="{animals}" labelField="custom" ></mx:ComboBox>
 
 
</mx:Application>

Open in new window

0
 

Author Comment

by:_CODER_
ID: 21807657
I see the changes. Trying it out now on my actual application...
0
 

Author Comment

by:_CODER_
ID: 21807730
It doesn't seem to be working in my application. I think it's because the combobox has a separate datasource then the method is using. but I need to match them based on Text in the label of the combo box is that possible?
0
 

Author Comment

by:_CODER_
ID: 21808007
if my datasource for the comobBox is from a database table, will it still be setup as:
private function onClick1(eventtester:Object):void

or will it be different?

0
 
LVL 23

Expert Comment

by:Siva Prasanna Kumar
ID: 21808283
that should not matter provided the item (object) which is going to onClick1 is same as the one in the combobox, but as you say they are different then you may have to do a little more processing like iterating the datasource used for that combo box and then setting the selected index accordingly.

0
 

Author Comment

by:_CODER_
ID: 21808351
When you say "providing it's the same" do you mean that its the same text?

0
 
LVL 23

Expert Comment

by:Siva Prasanna Kumar
ID: 21808474
what ever your feel is same must be mapped for example lets say both have some kind of uniq ID which will be same even thou there text's are different then you need to search for that uniqId in the data source of the combo box and get its index and set the selected index of the combox to appropriate index.
0
 

Author Comment

by:_CODER_
ID: 21808582
ahh. ok. I need to match them based on text in the label. not index or uniq ID.
any ideas? Thank you for helping me!

0
 
LVL 23

Expert Comment

by:Siva Prasanna Kumar
ID: 21808641
what was the label field?

your code will be something like this, where dp is the data provider for the combobox.
 private function onClick1(eventtester:Object):void {
                dataStr=eventtester.custom;
                
             //for loop iterating over the dataPrvider of the combobox
             //for every object in dataprovider check the text
             // like if(dp[i].text==dataStr)
                   {
                      animalsComboBox.selectedIndex = i;
                   }
               
            }

Open in new window

0
 

Author Comment

by:_CODER_
ID: 21809053
the label field of the dataprovider is DriverName

0
 
LVL 23

Expert Comment

by:Siva Prasanna Kumar
ID: 21809129
Dude its not possbile to tell like this :)

you may have to get the text on the clicked button and then get the data provider of the combox and iterate through them and match the text of the button with DriverName and set the matched index as selectedIndex
0
 
LVL 1

Accepted Solution

by:
KaiHogan earned 500 total points
ID: 23226936
OK, see if this is what you are looking for.   It is what I am doing to match up the combobox (from data source #1) with the DataGrid Element (Data Sourse #2).   So when ever the datagrid selected item is changed it calls a change function that looks at this to determine the correct selected item.
var i:int = -1;
for each ( var node : XML in Application.application.SV_base.lastResult.BaseList )
{
	i++;					
	if (node.Base == dg.selectedItem.Base)
	{
		userBaseEdit.selectedIndex = i;  
	}
}

Open in new window

0
 
LVL 29

Expert Comment

by:dgofman
ID: 37475267
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0

Featured Post

How to Defend Against the WCry Ransomware Attack

On May 12, 2017, an extremely virulent ransomware variant named WCry 2.0 began to infect organizations. Within several hours, over 75,000 victims were reported in 90+ countries. Learn more from our research team about this threat & how to protect your organization!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Flex Builder 4.5 3 427
Getting rid of an unwanted URL 5 251
Flash builder 101 tutorial 3 455
unable to get fla to publish to swf 4 313
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…
A big percent of today’s marketing activity is performed through the online environment. The marketing strategies that have existed a decade ago no longer relate to what’s happening today. We’re currently facing a revolutionary era, called the digit…
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…

734 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