<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>FlexOOP &#187; combobox</title>
	<atom:link href="http://flexoop.com/tag/combobox/feed/" rel="self" type="application/rss+xml" />
	<link>http://flexoop.com</link>
	<description>Flex, AIR, ColdFusion, and everything in between</description>
	<lastBuildDate>Tue, 13 Jul 2010 03:56:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Deduped ComboBox Component</title>
		<link>http://flexoop.com/2008/11/deduped-combobox-component/</link>
		<comments>http://flexoop.com/2008/11/deduped-combobox-component/#comments</comments>
		<pubDate>Thu, 27 Nov 2008 02:52:49 +0000</pubDate>
		<dc:creator>Gareth</dc:creator>
				<category><![CDATA[Component]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[combobox]]></category>
		<category><![CDATA[dedupedComboBox]]></category>

		<guid isPermaLink="false">http://flexfusion.archfamily.com/?p=48</guid>
		<description><![CDATA[There are times that I have wanted to remove all duplicates from a combobox&#8217;s dataprovider, but haven&#8217;t wanted to requery the database or roll my own code snippet each time to remove those duplicates.  To make my life a little easier (and hopefully anyone else that may be in need of this feature), I&#8217;ve created [...]]]></description>
			<content:encoded><![CDATA[<p>There are times that I have wanted to remove all duplicates from a combobox&#8217;s dataprovider, but haven&#8217;t wanted to requery the database or roll my own code snippet each time to remove those duplicates.  To make my life a little easier (and hopefully anyone else that may be in need of this feature), I&#8217;ve created the DedupeComboBox class.</p>
<p>This handy little item allows you to set the dataprovider of the combobox to an array or arraycollection, and set the labelField value and the combobox will then handle the rest.  The labelfield is important as that is what allows the component to remove the duplicates from the arraycollection (if it is an array, it must also be an array of objects with properties that match the labelField or it will not remove anything).  Also, as the items populate the deduped dictionary instance, the value of that item in the arraycollection is saved to that &#8220;key&#8221; in the dictionary, allowing for retrieval of it later on if necessary.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">package com.<span style="color: #006600;">archfamily</span> <span style="color: #66cc66;">&#123;</span>
&nbsp;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">utils</span>.<span style="color: #006600;">Dictionary</span>;
&nbsp;
	<span style="color: #0066CC;">import</span> mx.<span style="color: #006600;">collections</span>.<span style="color: #006600;">ArrayCollection</span>;
	<span style="color: #0066CC;">import</span> mx.<span style="color: #006600;">controls</span>.<span style="color: #006600;">ComboBox</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> DedupeComboBox <span style="color: #0066CC;">extends</span> ComboBox <span style="color: #66cc66;">&#123;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">/**
		 * 
		 * Needed to override the standard dataprovider in order to reset
		 * the duplicate value each time
		 * 
		 */</span>
		override <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">set</span> dataProvider<span style="color: #66cc66;">&#40;</span> value:<span style="color: #0066CC;">Object</span> <span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
&nbsp;
			<span style="color: #000000; font-weight: bold;">var</span> _localArray:<span style="color: #0066CC;">Array</span> = <span style="color: #66cc66;">&#40;</span> value is ArrayCollection <span style="color: #66cc66;">&#41;</span> ? <span style="color: #66cc66;">&#40;</span> value as ArrayCollection <span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">source</span> : value as <span style="color: #0066CC;">Array</span>;
			<span style="color: #000000; font-weight: bold;">var</span> _map:Dictionary = <span style="color: #000000; font-weight: bold;">new</span> Dictionary<span style="color: #66cc66;">&#40;</span> <span style="color: #000000; font-weight: bold;">true</span> <span style="color: #66cc66;">&#41;</span>;
&nbsp;
			<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span> labelField.<span style="color: #0066CC;">length</span> <span style="color: #66cc66;">&gt;</span> <span style="color: #cc66cc;">0</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
				_localArray.<span style="color: #b1b100;">forEach</span><span style="color: #66cc66;">&#40;</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span> item:<span style="color: #66cc66;">*</span>, <span style="color: #0066CC;">index</span>:<span style="color: #0066CC;">int</span>, <span style="color: #0066CC;">array</span>:<span style="color: #0066CC;">Array</span> <span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
					_map<span style="color: #66cc66;">&#91;</span> item<span style="color: #66cc66;">&#91;</span> <span style="color: #0066CC;">this</span> <span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#93;</span> = item; <span style="color: #808080; font-style: italic;">// in the loop, this == labelField</span>
				<span style="color: #66cc66;">&#125;</span>, labelField <span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span>
&nbsp;
			<span style="color: #000000; font-weight: bold;">var</span> _returnArray:<span style="color: #0066CC;">Array</span> = <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span>;
			<span style="color: #b1b100;">for</span> <span style="color: #b1b100;">each</span> <span style="color: #66cc66;">&#40;</span> <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">object</span>:<span style="color: #0066CC;">Object</span> <span style="color: #b1b100;">in</span> _map <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
				_returnArray.<span style="color: #0066CC;">push</span><span style="color: #66cc66;">&#40;</span> <span style="color: #0066CC;">object</span> <span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span>
&nbsp;
			<span style="color: #0066CC;">super</span>.<span style="color: #006600;">dataProvider</span> = _returnArray;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> DedupeComboBox<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">super</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>then to implement it from your code you could do something as simple as this</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #66cc66;">&lt;</span>?<span style="color: #0066CC;">xml</span> <span style="color: #0066CC;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> encoding=<span style="color: #ff0000;">&quot;utf-8&quot;</span>?<span style="color: #66cc66;">&gt;</span>
<span style="color: #66cc66;">&lt;</span>mx:Application xmlns:mx=<span style="color: #ff0000;">&quot;http://www.adobe.com/2006/mxml&quot;</span> layout=<span style="color: #ff0000;">&quot;absolute&quot;</span> xmlns:archfamily=<span style="color: #ff0000;">&quot;com.archfamily.*&quot;</span><span style="color: #66cc66;">&gt;</span>
	<span style="color: #66cc66;">&lt;</span>mx:Script<span style="color: #66cc66;">&gt;</span>
		<span style="color: #66cc66;">&lt;!</span><span style="color: #66cc66;">&#91;</span>CDATA<span style="color: #66cc66;">&#91;</span>
			<span style="color: #0066CC;">import</span> mx.<span style="color: #006600;">collections</span>.<span style="color: #006600;">ArrayCollection</span>;
			<span style="color: #66cc66;">&#91;</span>Bindable<span style="color: #66cc66;">&#93;</span> <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> ac:ArrayCollection = <span style="color: #000000; font-weight: bold;">new</span> ArrayCollection<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#91;</span> <span style="color: #66cc66;">&#123;</span> <span style="color: #0066CC;">name</span>: <span style="color: #ff0000;">&quot;car&quot;</span>, total: <span style="color: #cc66cc;">100</span> <span style="color: #66cc66;">&#125;</span>,<span style="color: #66cc66;">&#123;</span> <span style="color: #0066CC;">name</span>: <span style="color: #ff0000;">&quot;bus&quot;</span>, total: <span style="color: #cc66cc;">200</span> <span style="color: #66cc66;">&#125;</span>,<span style="color: #66cc66;">&#123;</span> <span style="color: #0066CC;">name</span>: <span style="color: #ff0000;">&quot;bike&quot;</span>, total: <span style="color: #cc66cc;">300</span> <span style="color: #66cc66;">&#125;</span>,<span style="color: #66cc66;">&#123;</span> <span style="color: #0066CC;">name</span>: <span style="color: #ff0000;">&quot;train&quot;</span>, total: <span style="color: #cc66cc;">200</span> <span style="color: #66cc66;">&#125;</span>,<span style="color: #66cc66;">&#123;</span> <span style="color: #0066CC;">name</span>: <span style="color: #ff0000;">&quot;boat&quot;</span>, total: <span style="color: #cc66cc;">400</span> <span style="color: #66cc66;">&#125;</span>,<span style="color: #66cc66;">&#123;</span> <span style="color: #0066CC;">name</span>: <span style="color: #ff0000;">&quot;car&quot;</span>, total: <span style="color: #cc66cc;">500</span> <span style="color: #66cc66;">&#125;</span>,<span style="color: #66cc66;">&#123;</span> <span style="color: #0066CC;">name</span>: <span style="color: #ff0000;">&quot;bus&quot;</span>, total: <span style="color: #cc66cc;">200</span> <span style="color: #66cc66;">&#125;</span>,<span style="color: #66cc66;">&#123;</span> <span style="color: #0066CC;">name</span>: <span style="color: #ff0000;">&quot;convertible&quot;</span>, total: <span style="color: #cc66cc;">100</span> <span style="color: #66cc66;">&#125;</span>,<span style="color: #66cc66;">&#123;</span> <span style="color: #0066CC;">name</span>: <span style="color: #ff0000;">&quot;gizmo&quot;</span>, total: <span style="color: #cc66cc;">300</span> <span style="color: #66cc66;">&#125;</span>,<span style="color: #66cc66;">&#123;</span> <span style="color: #0066CC;">name</span>: <span style="color: #ff0000;">&quot;boat&quot;</span>, total: <span style="color: #cc66cc;">300</span> <span style="color: #66cc66;">&#125;</span>,<span style="color: #66cc66;">&#123;</span> <span style="color: #0066CC;">name</span>: <span style="color: #ff0000;">&quot;car&quot;</span>, total: <span style="color: #cc66cc;">100</span> <span style="color: #66cc66;">&#125;</span>,<span style="color: #66cc66;">&#123;</span> <span style="color: #0066CC;">name</span>: <span style="color: #ff0000;">&quot;dune buggy&quot;</span>, total:<span style="color: #cc66cc;">300</span> <span style="color: #66cc66;">&#125;</span>,<span style="color: #66cc66;">&#123;</span> <span style="color: #0066CC;">name</span>: <span style="color: #ff0000;">&quot;motorcycle&quot;</span>, total: <span style="color: #cc66cc;">100</span> <span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&gt;</span>
	<span style="color: #66cc66;">&lt;/</span>mx:Script<span style="color: #66cc66;">&gt;</span>
&nbsp;
	<span style="color: #66cc66;">&lt;</span>archfamily:DedupeComboBox id=<span style="color: #ff0000;">&quot;testing&quot;</span> dataProvider=<span style="color: #ff0000;">&quot;{ ac }&quot;</span> labelField=<span style="color: #ff0000;">&quot;name&quot;</span> <span style="color: #66cc66;">/&gt;</span>
<span style="color: #66cc66;">&lt;/</span>mx:Application<span style="color: #66cc66;">&gt;</span></pre></div></div>

<p>You can even get just the non-duplicate totals by changing labelField=&#8221;name&#8221; to labelField=&#8221;total&#8221;.</p>
]]></content:encoded>
			<wfw:commentRss>http://flexoop.com/2008/11/deduped-combobox-component/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

