Link to home
Start Free TrialLog in
Avatar of TURBOSHAN
TURBOSHAN

asked on

Get SelectedItem Flex ComboBox from XML DataProvider

This one is driving me bats!

I have a flex combo box that is populated from a xml data provider like this:

[Bindable]
private var xmlOfficerdata:ArrayCollection = new ArrayCollection();  

<mx:ComboBox   id="ofcCombo"  dataProvider={xmlOfficerdata}  />

         xmlOfficerdata = event.result.Officers.Officer;
         var officerObj:Object = new Object();
         officerObj.Officername = "*ALL";
         officerObj.OfficerID = 0;
         xmlOfficerdata.addItemAt(officerObj, 0);

And it works fine.   Now I want the user to be able to select one of the items from the combo box and then use the selected item (the OfficerID value in this case) for a query value.

It seems that no matter how I try to get that selected item, I get errors.

Here's what I have tried:

theIDselected = ofcCombo.selectedItem();  (error!!!!!)
theIDselected = ofcCombo.selecteditem.toString(); - returns [object object]
theIDselected = ofcCombo.selected.value;  (error!!!)
theIDselected - ofcCombo.selected.@value; (error!!!)

And some other variations but nothing works to give me the value.

Can anyone tell me how to get the value of  a combobox selected item (officerID in this example)when the dataprovider comes in as an ArrayCollection from an XML data provider?

Thanks!
         
Avatar of Gary Benade
Gary Benade
Flag of South Africa image

selectedItem in a property, not a function - take out the brackets:
theIDselected = ofcCombo.selectedItem;
Avatar of TURBOSHAN
TURBOSHAN

ASKER

Doesn't work.

With theIDselected as a data type of String,  I get this error when I use  your suggestion
 
Implicit coercion of a value with static type Object to a possibly unrelated type String.       
It should be

theIDselected = ofcCombo.selectedItem.OfficerID right? As selectedItem should be the object and I think you making object with parameters OfficerName and OfficerID.

Thanks,
Vindys
OK.  I think I follow you.  You're telling me that the data type then for selectedID should be Object,  right?

so:

private var theIDselected:Object;

I'll give this a try and let you know.
Yep. you might be able to get the data

var theIDSelected:Object = ofcCombo.selectedItem;

theIDSelected should be same as

var officerObj:Object = new Object();
         officerObj.Officername = "*ALL";
         officerObj.OfficerID = 0;

Thanks,
Vindys
If theIDselected is a String then this should work:

theIDselected = ofcCombo.selectedItem.OfficerID;
OK.  I'm still not getting the selected value.

Here are the details using the actual variable and object names (so I don't forget anything):

the dataprovider to the Combobox is returned to flex as type XML and added to an object named
officerObj like this:

           xmlOfficerdata = event.result.Officers.Officer;
         var officerObj:Object = new Object();
         officerObj.Officername = "*ALL";
         officerObj.OfficerID = 0;
         xmlOfficerdata.addItemAt(officerObj, 0);
         officerCombo.selectedIndex = 0;

When the user selects an item from the combo box...I store it in an object like this:

[Bindable]
       private var hldOfficerNumber:Object;
   
      hldOfficerNumber = officerCombo.selectedItem;


When I'm ready to use it, I want to pass it to an HTTPService paramater so that I can submit it to a PHP script to use with my SQL statement.  

I do that like this:

 var params:Object = {};
 params["searchOfficer"] =  StringUtil.trim(hldOfficerNumber.toString);


I added the "toString" on there to convert it to a string because otherwise I ge a type coercion error trying to move it to the params string.

When I run this,  I store the passed in values the PHP script is getting, so that I can look at them, and what I get for the "searchOfficer" paramater is:  function Function () {

I've only been using flex for about 2 weeks but I am already a huge fan of it.  It's just some little things like working with the combobox that are so easy to do in other languages, are throwing me a real curve here.  I suspect the problem is because of the way I am populating the combobox with the XML data in the very beginning and that I'm not getting that data cast correctly for the HTTPService call.  But I don't know what to do to fix this so that I can get the string value of the selected item from the combo box sent to my HTTPService.

HELP! :-)                
ASKER CERTIFIED SOLUTION
Avatar of Gary Benade
Gary Benade
Flag of South Africa 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
Yeah!  That got it!

Well, I had to also take out this code that was throwing it off:

var officerObj:Object = new Object();
officerObj.Officername = "*ALL";
officerObj.OfficerID = 0;
xmlOfficerdata.addItemAt(officerObj, 0);

That was causing me to always get a seleted ID of 0.  Not sure why.  

But yes, your example helped me very much!  

Thank you!

Really went to a lot of effort to help me understand this problem and how to resolve it.

Very professional!