Get SelectedItem Flex ComboBox from XML DataProvider

TURBOSHAN
TURBOSHAN used Ask the Experts™
on
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!
         
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
selectedItem in a property, not a function - take out the brackets:
theIDselected = ofcCombo.selectedItem;

Author

Commented:
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.       

Commented:
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
Success in ‘20 With a Profitable Pricing Strategy

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Author

Commented:
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.

Commented:
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;

Author

Commented:
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! :-)                
Here is a working example of what you are trying to do, you should be able to extract what you need:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init()">
	<mx:Script>
		<![CDATA[
			import mx.collections.ArrayCollection;
			import flash.net.URLLoader;
			import flash.net.URLRequest;
			import flash.net.URLRequestMethod;
			import flash.net.URLVariables;
			import flash.net.navigateToURL;
					
			[Bindable] private var xmlOfficerdata:ArrayCollection = new ArrayCollection();
			private var hldOfficerNumber:int;
		
			private function init():void
			{
				xmlOfficerdata.addItem( {Officername:"Bob", OfficerID:1});
				xmlOfficerdata.addItem( {Officername:"Harry", OfficerID:2});
				xmlOfficerdata.addItemAt( {Officername:"*All", OfficerID:0}, 0);
			}
			
			private function handleChange():void
			{
				hldOfficerNumber = cbOfficerSelect.selectedItem.OfficerID;
				trace('selected officerID=', hldOfficerNumber);
			}
			
			private function doSend():void
			{
				var urlv:URLVariables = new URLVariables();
				urlv.officerid = hldOfficerNumber;
				var url:URLRequest = new URLRequest();
				url.method = URLRequestMethod.GET;
				url.data = urlv;
				url.url = "http://localhost/foo.php";
				// you should get a window with your officerid in the params, change to your httpservice
				navigateToURL( url, "blank");				
			}
		]]>
	</mx:Script>
	<mx:ComboBox id="cbOfficerSelect" dataProvider="{xmlOfficerdata}" labelField="Officername" change="handleChange()" width="210"/>
	<mx:Button x="10" y="30" label="Send" click="doSend()"/>
</mx:Application>

Open in new window

Author

Commented:
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!

Author

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

Very professional!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial