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:ArrayCollec tion = new ArrayCollection();
<mx:ComboBox id="ofcCombo" dataProvider={xmlOfficerda ta} />
xmlOfficerdata = event.result.Officers.Offi cer;
var officerObj:Object = new Object();
officerObj.Officername = "*ALL";
officerObj.OfficerID = 0;
xmlOfficerdata.addItemAt(o fficerObj, 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.toSt ring(); - 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!
I have a flex combo box that is populated from a xml data provider like this:
[Bindable]
private var xmlOfficerdata:ArrayCollec
<mx:ComboBox id="ofcCombo" dataProvider={xmlOfficerda
xmlOfficerdata = event.result.Officers.Offi
var officerObj:Object = new Object();
officerObj.Officername = "*ALL";
officerObj.OfficerID = 0;
xmlOfficerdata.addItemAt(o
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.toSt
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!
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.
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.Offi cerID right? As selectedItem should be the object and I think you making object with parameters OfficerName and OfficerID.
Thanks,
Vindys
theIDselected = ofcCombo.selectedItem.Offi
Thanks,
Vindys
ASKER
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.
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
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.Offi cerID;
theIDselected = ofcCombo.selectedItem.Offi
ASKER
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.Offi cer;
var officerObj:Object = new Object();
officerObj.Officername = "*ALL";
officerObj.OfficerID = 0;
xmlOfficerdata.addItemAt(o fficerObj, 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(hldOfficer Number.toS tring);
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 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.Offi
var officerObj:Object = new Object();
officerObj.Officername = "*ALL";
officerObj.OfficerID = 0;
xmlOfficerdata.addItemAt(o
officerCombo.selectedIndex
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(hldOfficer
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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(o fficerObj, 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!
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(o
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!
ASKER
Really went to a lot of effort to help me understand this problem and how to resolve it.
Very professional!
Very professional!
theIDselected = ofcCombo.selectedItem;