The Web Squeeze: Flex/as3 Dynamic Combobox Class - The Web Squeeze

Jump to content

Forum

Flash and Multimedia

Adobe Flash help forum for teaching and learning coding methods for actionscript, flash slide shows, and flash animation. Want to squeeze out a flash website and need to understand better Flash SEO techniques? Here's the place to do it.
Digg Del.ico.us Slashdot Technorati furl Reddit Facebook Fark Google Magnolia Wink Yahoo Netscape
Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Flex/as3 Dynamic Combobox Class This thing is killing me!

#1 User is offline   rewake Icon

  • Rapid Squeezer
  • Icon
  • Group: Mentor
  • Posts: 429
  • Joined: 14-February 08
  • Gender:Male
  • Location:NY, USA

Posted 30 April 2009 - 03:53 PM

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.)

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

Quote

if ($name=='will') echo '/(bb|[^b]{2})/';

Raineri Jewelers | MySpace | Facebook | deviantART
0

#2 User is offline   djeyewater Icon

  • Rapid Squeezer
  • PipPipPipPip
  • Group: Members
  • Posts: 340
  • Joined: 18-February 08

Posted 02 May 2009 - 09:34 AM

What happens if instead of doing
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:
Alert.show(ObjectUtil.toString(this.dataProvider));


Wouldn't
Alert.show(this.dataProvider.toString());

show you the data?

Dave
0

#3 User is offline   rewake Icon

  • Rapid Squeezer
  • Icon
  • Group: Mentor
  • Posts: 429
  • Joined: 14-February 08
  • Gender:Male
  • Location:NY, USA

Posted 05 May 2009 - 01:41 PM

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

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

import mx.utils.ObjectUtil


Thanks,
Rich

Quote

if ($name=='will') echo '/(bb|[^b]{2})/';

Raineri Jewelers | MySpace | Facebook | deviantART
0

#4 User is offline   rewake Icon

  • Rapid Squeezer
  • Icon
  • Group: Mentor
  • Posts: 429
  • Joined: 14-February 08
  • Gender:Male
  • Location:NY, USA

Posted 06 May 2009 - 09:07 AM

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

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();
		}
	}
}

Quote

if ($name=='will') echo '/(bb|[^b]{2})/';

Raineri Jewelers | MySpace | Facebook | deviantART
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic


Page 1 of 1
Trackbacks
Trackback URL Trackback Date Total Hits
No trackbacks were found
Page 1 of 1

Similar Topics
  Topic Started By Stats Last Post Info
New Replies Icon Hello From A Memphis Flex/coldfusion Guru
Not much of a message-board writer, but thought it polite to say "
ChuckWilliams Icon
  • 6 Replies
  • 1,245 Views
New Replies Icon Flash: Design Vs Coding... Flex? rewake Icon
  • 1 Replies
  • 2,102 Views
New Replies Icon Db Class
Tell me what you think
rewake Icon
  • 2 Replies
  • 724 Views
New Replies Icon Dynamic Stylesheet Switcher In Php
Tutorial discussion thread
Squeeze Bot Icon
  • 0 Replies
  • 515 Views
New Replies Icon Killing An Unnecessary Scrollbar
CSS perfectionism, part one of two
MikeHopley Icon
  • 12 Replies
  • 1,216 Views

2 User(s) are reading this topic
0 members, 2 guests, 0 anonymous users