Solved

How do I loop through ItemRenderers in a custom ComboBox?

Posted on 2009-05-12
5
2,629 Views
Last Modified: 2013-11-11
I have a custom ComboBox component where each item consists of a checkbox and a label. I need to be able to clear all selected checkboxes when needed, but I can't figure out how to access the ItemRenderers when looping through the list of selected items.
public function deselectAll( ):void
{
    	for( var i in selectedItems )
    	{
    	       // what do I do here?
    	}
}

Open in new window

0
Comment
Question by:eCat12
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 10

Expert Comment

by:mplord
ID: 24373031
The best way to do this is to think in terms of manipulating the data that the combobox and itemrenderers are presenting, rather than manipulating the GUI directly to alter the data.

To explain, you presumably provide an ArrayCollection or XMLList to the Combobox dataprovider.
Each item should have a property related to whether it is 'selected' or otherwise.
Your itemrenderer checkbox state should be data-bound to the 'selected' property.

This way if the user changes the state of a checkbox by clicking, the underlying 'selected' property for that item data is updated according to the checkbox state.

Clearing checkboxes is then a simple case of iterating through the dataprovider items to clear the 'selected' property, instead of trying to manipulate the data through the GUI as a user would.
0
 
LVL 37

Expert Comment

by:zzynx
ID: 24374834
>> I can't figure out how to access the ItemRenderers
Don't try to look for itemrenderer*S*.
There's only ONE itemrenderer that renders all the items in your combo box.

You should indeed simply iterate through all the items in your combo box's dataprovider and reset property 'X' to false or 0 or whatever.
Property 'X' is the property that corresponds with a selected checkbox in your itemrenderer.
0
 

Author Comment

by:eCat12
ID: 24377608
Thanks for the replies. I looped through the items in the dataProvider and set the selected property of each item to false, but the checkboxes still remain checked until I close the combobox and re-open it. How do bind this value to the checkbox's state?
0
 
LVL 10

Accepted Solution

by:
mplord earned 100 total points
ID: 24380171
This can be tricky with Checkboxes - here's an example.
In this case, the source property 'checked' is a string "true" or "false", where the Checkbox 'selected' property requires boolean true/false, so there's some translation between the two types going on here.


<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
 
	<mx:Script>
		<![CDATA[
			[Bindable] public var model:XML = 
				<items>
					<person name="Fred" checked="true"/>
					<person name="Jim" checked="false"/>
					<person name="Bill" checked="false"/>
					<person name="John" checked="false"/>
				</items>
				
			private function onClear(event:Event):void
			{
				for each (var node:XML in combo.dataProvider)
					node.@checked = "false";
			}
			
		]]>
	</mx:Script>
 
	<mx:VBox verticalCenter="0" horizontalCenter="0" horizontalAlign="center">
		<mx:ComboBox id="combo" labelField="@name" dataProvider="{model.person}">
			<mx:itemRenderer>
				<mx:Component>
					<mx:CheckBox label="test" width="100%" change="cbChange(event);" creationComplete="init();">
						<mx:Script>
						<![CDATA[
							import mx.binding.utils.BindingUtils;
 
							private function init():void
							{
								BindingUtils.bindProperty(this, "selected", this, "checked");
								this.label = data.@name;
							}
						
							[Bindable] public function get checked():Boolean
							{
								return (data.@checked=="true")?true:false;
							}
							
							public function set checked(state:Boolean):void
							{
								data.@checked = state?"true":"false";
							}
						
							private function cbChange(event:Event):void
							{
								checked = this.selected;
								event.stopPropagation();
							}
						]]>
					</mx:Script>
						
					</mx:CheckBox>
				</mx:Component>
				
			</mx:itemRenderer>
		</mx:ComboBox>
		<mx:Button label="Clear" click="onClear(event);"/>
	</mx:VBox>
	
</mx:Application>

Open in new window

0
 

Author Comment

by:eCat12
ID: 24399465
Thank you!! I've got it now. :)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Crash with shockwave flash 8 254
Rotating Adobe pdf document 4 329
Install adobe flash plugin for Firefox win 7 9 71
Play RTMP without Flash 3 747
First things first - Preparation We need all the part for this install and it's much nicer to have them all on hand when you need them so here's what's required. Download Eclipse 3.5 32 bit (I like the Classic flavour) from here. (http://www.e…
I have been doing hardcore actionscripting for some time; and needless to say I have faced a lot of problems in just understanding others' code rather than understanding what the code executes. A programmer's life can become hell when there are a lo…
The goal of the tutorial is to teach the user how to set there setting in Adobe Flash Media Live Encoder and YouTube for optimal video and audio quality.
The goal of the tutorial is to teach the user how to select which audio input to use. Once you have an audio input plugged into the laptop or computer, you will go into the audio input settings and choose which audio input you want to use.

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question