Welcome Guest!
Please login
If you do not have an account yet on The Web Squeeze forums, please Register! It's FREE and there are many benefits:
- Receive Fast Advice
- Learn Programming Languages
- Get Professional Website Reviews
- Quick Troubleshooting Assistance
Flex/as3 Dynamic Combobox Class
This is a discussion on Flex/as3 Dynamic Combobox Class, within the Graphics, Flash, and Multimedia section. This forum and the thread "Flex/as3 Dynamic Combobox Class" are both part of the Designing Your Website category.
![]() ![]() |
Apr 30 2009, 03:53 PM
Post
#1
|
|
![]() Rapid Squeezer ![]() Posts: 427 Joined: 14-February 08 From: NY, USA |
Hey everyone,
I've been banging my head with this class & I'm hoping someone can help me figure out what I'm doing wrong! Free cookie to whoever helps me solve it!! The basic idea is that I have an extended component ComboBox that gets filled with data from a DB. The only thing I can't seem to get working is the setIndex() function, which is supposed to set the selected index of the ComboBox to the appropriate selected index. Problem is, when trying to access the dataProvider of the ComboBox from the setIndex() method dataProvider is empty. Here's the thing... if I set the dataProvider to something Bindable it will work, but otherwise it doesn't. Here's the code... (I've added some comments to help you walk through it.) CODE package com.lib { import mx.collections.ArrayCollection; import mx.controls.Alert; import mx.controls.ComboBox; import mx.events.FlexEvent; import mx.events.ListEvent; import mx.rpc.events.FaultEvent; import mx.rpc.events.ResultEvent; import mx.rpc.http.HTTPService; import mx.utils.ObjectUtil; public class DynComboBox extends ComboBox { private var savedSelectedIndex:int; public var selectionChanged:Boolean = false; // stuff to get the data public var type:String; public var selected:String; [Bindable] private var fDataLoader:HTTPService = new HTTPService(); private var params:Object = new Object(); public function DynComboBox() { super(); this.enabled = false; this.addEventListener(ListEvent.CHANGE, onChange); this.addEventListener(FlexEvent.CREATION_COMPLETE, getData); } private function getData(e:FlexEvent):void { this.removeEventListener(FlexEvent.CREATION_COMPLETE, getData); savedSelectedIndex = this.selectedIndex; // This gets the form data from the DB, such as the // 50 states for instance fDataLoader.method = "POST"; fDataLoader.url = "http://192.168.1.59/index.php/lib/formData"; params.filter = type; fDataLoader.send(params); fDataLoader.addEventListener(ResultEvent.RESULT, fillComboBox); fDataLoader.addEventListener(FaultEvent.FAULT, fdlError); } private function fdlError(err:FaultEvent):void { Alert.show("Could not load form data"); } public function fillComboBox(res:ResultEvent):void { // This works fine... takes the result and populates the ComboBox // with the data. var sts:ArrayCollection = res.result.root.formData as ArrayCollection; this.dataProvider = sts; this.enabled=true; } private function onChange(event:ListEvent):void { savedSelectedIndex = this.selectedIndex; // I have this commented, but it works. When you select an // item from the ComboBox it alerts the correct data. //Alert.show(this.dataProvider[selectedIndex].data.toString()); } public function set setIndex(si:String):void { /** * Here's where I run into problems. When showing the * dataProvider I get an empty array. * */ Alert.show(ObjectUtil.toString(this.dataProvider)); } override public function set dataProvider(dp:Object):void { this.selectedIndex = savedSelectedIndex; } } } Rich -------------------- |
|
|
May 2 2009, 09:34 AM
Post
#2
|
|
|
Rapid Squeezer ![]() ![]() ![]() ![]() Posts: 308 Joined: 18-February 08 |
What happens if instead of doing
CODE this.dataProvider = sts; You loop through the arrayCollection and use the dataProvider's addItem() method to add the data. I think you probably have to do that or bind the dataProvider to sts. Also I am not sure about this: CODE Alert.show(ObjectUtil.toString(this.dataProvider)); Wouldn't CODE Alert.show(this.dataProvider.toString()); show you the data? Dave |
|
|
May 5 2009, 01:41 PM
Post
#3
|
|
![]() Rapid Squeezer ![]() Posts: 427 Joined: 14-February 08 From: NY, USA |
Hey Dave,
QUOTE You loop through the arrayCollection and use the dataProvider's addItem() method to add the data. I think you probably have to do that or bind the dataProvider to sts. I'm gonna give this a try and see if it works... I'll let you know! QUOTE CODE Alert.show(this.dataProvider.toString()); I've been using the object utils lately for outputting info instead of toString()... it's a bit easier for me and shows more data, which I like. CODE import mx.utils.ObjectUtil Thanks, Rich -------------------- |
|
|
May 6 2009, 09:07 AM
Post
#4
|
|
![]() Rapid Squeezer ![]() Posts: 427 Joined: 14-February 08 From: NY, USA |
Ok, got it worked out! Bascially what I found was that the data in the dataProvider was there, but just not when I needed it to be. In other words, when the combo box is instantiated it runs all the public set functions. The problem is that the dataProvider is not set just yet (it actually gets set twice - once on creation and then again when I set it to the HTTPService result data), and shows an empty array. After moving a few things around the dynamic combo box is good to go. Here's the updated code...
CODE package com.lib
{ import mx.collections.ArrayCollection; import mx.controls.Alert; import mx.controls.ComboBox; import mx.events.FlexEvent; import mx.rpc.events.FaultEvent; import mx.rpc.events.ResultEvent; import mx.rpc.http.HTTPService; public class DynComboBox extends ComboBox { public var type:String; public var selected:String; [Bindable] private var fDataLoader:HTTPService = new HTTPService(); private var params:Object = new Object(); public function DynComboBox() { super(); this.enabled = false; this.addEventListener(FlexEvent.CREATION_COMPLETE, getData); } private function getData(e:FlexEvent):void { this.removeEventListener(FlexEvent.CREATION_COMPLETE, getData); // This url would be the path to the page that generates your data fDataLoader.url = parentApplication.config.serverLocation+"/lib/formData"; fDataLoader.method = "POST"; params.filter = type; fDataLoader.send(params); fDataLoader.addEventListener(ResultEvent.RESULT, fillComboBox); fDataLoader.addEventListener(FaultEvent.FAULT, fdlError); } private function fdlError(err:FaultEvent):void { Alert.show("Could not load form data"); } private function fillComboBox(res:ResultEvent):void { var sts:ArrayCollection = res.result.root.formData as ArrayCollection; this.dataProvider = sts; this.enabled=true; } private function setIndex():void { if (this.dataProvider.length > 0) { for (var i:int; i<this.dataProvider.length; i++) { if (this.dataProvider[i].data.toString() == selected) { this.selectedIndex = i; break; } } } } override public function set dataProvider(dp:Object):void { super.dataProvider = dp; setIndex(); } } } -------------------- |
|
|
If you found The Web Squeeze to be helpful, please donate so we can keep this site FREE, FRESH, and fortified with Web Design & Development info!
![]() ![]() |
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:
Similar Topics
| Topic Title | Replies | Topic Starter | Views | Last Action | |||
|---|---|---|---|---|---|---|---|
![]() |
6 | ChuckWilliams | 1,200 | 14th February 2008 - 03:57 PM Last post by: Ross |
|||
![]() |
1 | rewake | 1,990 | 14th February 2008 - 10:31 PM Last post by: Jacob |
|||
![]() |
2 | rewake | 667 | 21st February 2008 - 02:09 AM Last post by: Rakuli |
|||
![]() |
0 | Squeeze Bot | 479 | 3rd April 2008 - 09:49 AM Last post by: Squeeze Bot |
|||
![]() |
5 | overthemike | 1,019 | 6th November 2008 - 07:30 PM Last post by: japh |
|||







Apr 30 2009, 03:53 PM







