This works because strings are compared by value. It wouldn't work for other
objects, which are compared by reference!
var s1:String = "My String";
var s2:String = "My String";
s1 == s2 returns true, this will work!
var l1:Label = new Label();
l1.text = "My String";
var l2:Label = new Label();
l2.text = "My String";
l1 == l2 returns false, this won't work!
On Thu, Oct 9, 2008 at 3:52 PM, valdhor <[EMAIL PROTECTED]> wrote:
> Tracy
>
> The ComboBox dataProvider is set from a RemoteObject call that returns an
> array of strings that are the unique "Platforms" in the database.
>
> eg. SELECT Platform FROM theDB WHERE Platform IS NOT NULL ORDER BY Platform
> while ($row = fetchAssoc())
> { $platformsArray[] = $row['Platform'] }
> return array_unique($platformsArray)
>
> Further on in the code I get an ID from an event sent to this component. I
> make a call on the database which returns an SDActionItem object (DTO) that
> has two string fields and two int fields. One of the string fields is the
> "Platform" that this SDActionItem object belongs to.
>
> There is no reference between the ComboBox dataProvider and
> SDActionItem.platform. The only correlation between them is that
> SDActionItem.platform string is the same (textually) as one of the items in
> the ComboBox dataProvider.
>
>
> When I get this back I want to set the ComboBox selectedItem to this string
> so I do:
>
> platformComboBox.selectedItem = SDActionItem.platform;
>
> This works but I don't know why (Other than it makes sense to me that it
> should work this way).
>
> I put together a little demo. Should it or shouldn't it work? As you can
> see currentSelection is a string set to "Scotland" initially. There is no
> reference to the comboBox dataprovider. When the comboBox is created, its
> selectedItem is set to this string.
>
> <?xml version="1.0" encoding="utf-8"?>
> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
> layout="vertical">
> <mx:Script>
> <![CDATA[
> import mx.controls.ComboBox;
>
> private var currentSelection:String = "Scotland";
> private var countryComboBox:ComboBox;
>
> private function addCombo():void
> {
> countryComboBox = new ComboBox();
> countryComboBox.dataProvider = countryAC;
> countryComboBox.rowCount = countryAC.length;
> countryComboBox.selectedItem = currentSelection;
> countryFormItem.addChild(countryComboBox);
> }
>
> private function removeCombo():void
> {
> currentSelection = countryComboBox.selectedItem.toString();
> countryFormItem.removeChild(countryComboBox);
> }
> ]]>
> </mx:Script>
> <mx:ArrayCollection id="countryAC">
> <mx:String>United States</mx:String>
> <mx:String>Australia</mx:String>
> <mx:String>England</mx:String>
> <mx:String>Ireland</mx:String>
> <mx:String>Scotland</mx:String>
> <mx:String>Wales</mx:String>
> </mx:ArrayCollection>
> <mx:Form>
> <mx:FormItem id="countryFormItem" label="Country: "
> fontWeight="bold"/>
> </mx:Form>
> <mx:Button label="Add Combo" click="addCombo()"/>
> <mx:Button label="Remove Combo" click="removeCombo()"/>
> </mx:Application>
>
>
> --- In [email protected], "Tracy Spratt" <[EMAIL PROTECTED]> wrote:
> >
> > No, that should not work unless SDActionItem.platform is a reference to
> > an item in the ComboBox dataProvider.
> >
> >
> >
> > Tracy
> >
> >
> >
> >
> >
> >
> >
> > ________________________________
> >
> > From: [email protected] [mailto:[EMAIL PROTECTED] On
> > Behalf Of valdhor
> > Sent: Wednesday, October 08, 2008 12:31 PM
> > To: [email protected]
> > Subject: [flexcoders] Re: ComboBox in Component won't open to Saved
> > Value
> >
> >
> >
> > Tracy
> >
> > I must be missing something here.
>
> >
> > I have a combobox with a dataprovider of an arraycollection that is
> > returned from a MySQL database. This is populated on creationcomplete.
> > Once this arrayCollection is populated I make another call to get
> > specific data. Part of this data is a field that corresponds to an
> > item in the combobox.
> >
> > When the second call returns, all I do is:
> >
> > platformComboBox.selectedItem = SDActionItem.platform;
> >
> > SDActionItem is the DTO returned from the second call and platform is
> > one of it's fields that correspond to an item in the combobox.
> >
> > This works every time and I do not have to loop through the data
> > provider.
> >
> > Are you telling me that this should not work?
> >
> > --- In [email protected]
> > <mailto:flexcoders%40yahoogroups.com<flexcoders%2540yahoogroups.com>
> >
> > , "Tracy Spratt" tspratt@ wrote:
> > >
> > > ComboBox.selectedItem will only work if you assign a *reference to an
> > > item in the ComboBox dataProvider*. This is rarely possible and is not
> > > in your case.
> > >
> > >
> > >
> > > What you must do is loop over the items in the CobmoBox.dataProvider
> > and
> > > compare the appropriate property's value to the value you want to
> > match.
> > > When a match is found, use the loop indes to set the ComboBox's
> > > selectedIndex. This is simple enough to do in a one-off situation.
> > >
> > >
> > >
> > > If you need this often, then you might want to use an extended
> > ComboBox.
> > > Making a generic one is a bit tricky, because the Combo Box
> > dataProvider
> > > items can have any properties at all. I have an example on
> > > www.cflex.net <http://www.cflex.net/ <http://www.cflex.net/> > (it
> > allows you to set the data
> > > field you want to match) and Ben forta has done one and there are
> > > others.
> > >
> > >
> > >
> > > Tracy
> > >
> > >
> > >
> > > ________________________________
> > >
> > > From: [email protected]
> > > <mailto:flexcoders%40yahoogroups.com<flexcoders%2540yahoogroups.com>
> >
> > [mailto:[email protected]
> > <mailto:flexcoders%40yahoogroups.com<flexcoders%2540yahoogroups.com>
> >
> > ] On
> > > Behalf Of Dan Pride
> > > Sent: Wednesday, October 08, 2008 10:01 AM
> > > To: [email protected]
> > > <mailto:flexcoders%40yahoogroups.com<flexcoders%2540yahoogroups.com>>
>
> > > Subject: [flexcoders] ComboBox in Component won't open to Saved Value
> > >
> > >
> > >
> > > Hi
> > > I have a ComboBox used as a Popup on a form component.
> > > It saves fine using the following function
> > >
> > > private function closeGenderPop(event:Event):void {
> > > ComboBox(event.target).selectedItem.label};
> > >
> > >
> > > I want to have it display the stored value the next time it is opened.
> > > I tried this but no luck.
> > >
> > > private function openGenderPop(event:Event):void {
> > > genderPop.selectedIndex = 1;
> > > ComboBox(event.target).selectedItem.label; }
> > >
> > > Really appreciate the help
> > >
> > > Dan Pride
> > >
> >
>
>
--
Haykel Ben Jemia
Allmas
Web & RIA Development
http://www.allmas-tn.com