<?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>Kalen Gibbons &#187; ArrayCollection</title>
	<atom:link href="http://www.kalengibbons.com/blog/index.php/tag/arraycollection/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.kalengibbons.com/blog</link>
	<description>The Dead Tree Blog</description>
	<lastBuildDate>Mon, 24 Oct 2011 20:43:10 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Filtering a Flex Tree using an ArrayCollection</title>
		<link>http://www.kalengibbons.com/blog/index.php/2009/01/filtering-a-flex-tree-using-an-arraycollection/</link>
		<comments>http://www.kalengibbons.com/blog/index.php/2009/01/filtering-a-flex-tree-using-an-arraycollection/#comments</comments>
		<pubDate>Wed, 07 Jan 2009 04:03:45 +0000</pubDate>
		<dc:creator>Kalen Gibbons</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[How-To]]></category>
		<category><![CDATA[ArrayCollection]]></category>
		<category><![CDATA[Filtering]]></category>
		<category><![CDATA[Recursion]]></category>
		<category><![CDATA[Tree]]></category>

		<guid isPermaLink="false">http://www.kalengibbons.com/blog2/?p=89</guid>
		<description><![CDATA[Flex Trees can be difficult to filter because of their hierarchical nature. One possible way to do it is to use an ArraryCollection as your dataProvider, with each node having a &#8220;children&#8221; property that is also an ArrayCollection. The tricky part in filtering the Tree is making sure the children are filtered in addition to [...]]]></description>
			<content:encoded><![CDATA[<p>Flex Trees can be difficult to filter because of their hierarchical nature. One possible way to do it is to use an ArraryCollection as your dataProvider, with each node having a &#8220;children&#8221; property that is also an ArrayCollection.</p>
<p>The tricky part in filtering the Tree is making sure the children are filtered in addition to the root nodes.&nbsp; So, in the example below, I have created a function that will loop through each node and its children recursively. </p>
<p>It is important to note that the filtering takes place from the bottom up, meaning that a node&#8217;s children are always filtered before the node itself.&nbsp; This is important, because normally a filter removes everything that does not match a certain criteria.&nbsp; In our case, to remain in the collection, a node must match a certain criteria OR have children that do.</p>
<div class="code">
        &lt;?xml version=&#8221;1.0&#8243; encoding=&#8221;utf-8&#8243;?&gt;<br />
            <span class="mxmlComponent">&lt;mx:Application</span> xmlns:mx=&#8221;<span class="asString">http://www.adobe.com/2006/mxml</span>&#8221;</p>
<div class="indent">layout=&#8221;<span class="asString">absolute</span>&#8220;<span class="mxmlComponent">&gt;</span></div>
<div class="indent">
			<span class="mxmlSpecialString"><br />
			<br />
			&lt;mx:Script&gt;</span></p>
<div class="indent">
    		&lt;![CDATA[<br />
            <span class="asReserved">import</span> vo.Person;<br />
            <span class="asReserved">import</span> mx.collections.ArrayCollection;</p>
<p>            [<span class="asReserved">Bindable</span>]<br />
            <span class="asReserved">private</span> <span class="asVar">var</span> people:ArrayCollection = <span class="asReserved">new</span> ArrayCollection([</p>
<div class="indent">
			<span class="asReserved">new</span> Person("<span class="asString">Grandma Susan</span>", <span class="asReserved">new</span> ArrayCollection([</p>
<div class="indent">
			<span class="asReserved">new</span> Person("<span class="asString">John</span>", <span class="asReserved">new</span> ArrayCollection([</p>
<div class="indent">
			<span class="asReserved">new</span> Person("<span class="asString">Timmy</span>"),<br />
            <span class="asReserved">new</span> Person("<span class="asString">Sammy</span>"),<br />
            <span class="asReserved">new</span> Person("<span class="asString">Alan</span>")</div>
<p>			])),<br />
            <span class="asReserved">new</span> Person(&#8220;<span class="asString">Tiffany</span>&#8220;, <span class="asReserved">new</span> ArrayCollection([</p>
<div class="indent">
			<span class="asReserved">new</span> Person("<span class="asString">Billy</span>"),<br />
            <span class="asReserved">new</span> Person("<span class="asString">Adam</span>"),<br />
            <span class="asReserved">new</span> Person("<span class="asString">Graham</span>"),<br />
            <span class="asReserved">new</span> Person("<span class="asString">Vennesa</span>")</div>
<p>			])),<br />
            <span class="asReserved">new</span> Person(&#8220;<span class="asString">Michael</span>&#8220;, <span class="asReserved">new</span> ArrayCollection([</p>
<div class="indent">
			<span class="asReserved">new</span> Person("<span class="asString">Jannette</span>"),<br />
            <span class="asReserved">new</span> Person("<span class="asString">Alan</span>", <span class="asReserved">new</span> ArrayCollection([</p>
<div class="indent">
			<span class="asReserved">new</span> Person("<span class="asString">Alice</span>"),<br />
            <span class="asReserved">new</span> Person("<span class="asString">Jane</span>")</div>
<p>			]))			</p></div>
<p>			])),<br />
            <span class="asReserved">new</span> Person(&#8220;<span class="asString">Peter</span>&#8220;),</p>
<div class="indent">
			<span class="asReserved">new</span> Person(&#8220;<span class="asString">Cindy</span>&#8220;, <span class="asReserved">new</span> ArrayCollection([</p>
<div class="indent">
			<span class="asReserved">new</span> Person("<span class="asString">Paul</span>"),<br />
            <span class="asReserved">new</span> Person("<span class="asString">David</span>"),<br />
            <span class="asReserved">new</span> Person("<span class="asString">Joseph</span>"),<br />
            <span class="asReserved">new</span> Person("<span class="asString">Cameron</span>"),<br />
            <span class="asReserved">new</span> Person("<span class="asString">Debra</span>"),<br />
            <span class="asReserved">new</span> Person("<span class="asString">Polly</span>")
			</div>
</p></div>
<p>			]))</p></div>
<p>            ])) </p></div>
<p>            ]);</p>
<p>            <span class="asReserved">private</span> <span class="asFunction">function</span> refreshData():<span class="asReserved">void</span>{</p>
<div class="indent">
			<span class="asComment">//reset the root node to its original unfiltered data</span><br />
            people[0].children = <span class="asReserved">new</span> ArrayCollection(people[0].children.source);<br />
            <span class="asComment">//start the recursion at the root node</span><br />
            refreshRecursiveChildren(people.source[0]);<br />
            <span class="asComment">//update the Tree after the data has been filtered</span><br />
            personsTree.invalidateList();</div>
<p>            }</p>
<p>            <span class="asReserved">private</span> <span class="asFunction">function</span> refreshRecursiveChildren(person:Person):<span class="asReserved">void</span>{</p>
<div class="indent">
			<span class="asReserved">if</span>(person.children){</p>
<div class="indent">
			<span class="asComment">//loop through each child and filter its children</span><br />
            <span class="asReserved">for each</span>(<span class="asVar">var</span> _person:Person <span class="asReserved">in</span> person.children.source){</p>
<div class="indent">refreshRecursiveChildren(_person);</div>
<p>            }<br />
            <span class="asComment">//reset each &#8220;children&#8221; ArrayCollection to its original unfiltered data</span><br />
            person.children = <span class="asReserved">new</span> ArrayCollection(person.children.source);<br />
            <span class="asComment">//set the filterfunction for the newly updated node</span><br />
            person.children.filterFunction = filterData;<br />
            <span class="asComment">//run the fitlerFunction</span><br />
            person.children.refresh();			</div>
<p>            }		</p></div>
<p>            }</p>
<p>            <span class="asReserved">public</span> <span class="asFunction">function</span> filterData(item:Object):Boolean{</p>
<div class="indent">
			<span class="asComment">//get the string to filter the nodes by</span><br />
            <span class="asVar">var</span> searchString:String = iNameFilter.text;<br />
            <span class="asComment">//if string is found in node return true.<br />
            //since the recursive filtering takes place from bottom up, if<br />
            //a collection still has children after filtering, also return true</span><br />
            <span class="asReserved">if</span>(searchString.length == 0
<div class="indent">|| item.name.toLowerCase().indexOf(searchString.toLowerCase()) &gt;= 0)<br />
            <span class="asReserved">return true</span>;</div>
<p>            <span class="asReserved">else if</span>(item.children != <span class="asReserved">null</span> &amp;&amp; item.children.length &gt; 0)</p>
<div class="indent"><span class="asReserved">return true</span>;</div>
<p>
            <span class="asReserved">return false</span>;</div>
<p>			}<br />
            ]]&gt;</div>
<p>            <span class="mxmlSpecialString">&lt;/mx:Script&gt;</span></p>
<p>    <span class="mxmlComponent">&lt;mx:VBox</span> width=&#8221;<span class="mxmlString">100%</span>&#8221; height=&#8221;<span class="mxmlString">100%</span>&#8220;</p>
<div class="indent4">
	paddingTop=&#8221;<span class="mxmlString">10</span>&#8220;<br />
	paddingBottom=&#8221;<span class="mxmlString">10</span>&#8220;<br />
    paddingLeft=&#8221;<span class="mxmlString">5</span>&#8220;<br />
    paddingRight=&#8221;<span class="mxmlString">5</span>&#8220;<span class="mxmlComponent">&gt;</span>
    </div>
<div class="indent">
	<span class="mxmlComponent">&lt;mx:Tree</span> id=&#8221;<span class="mxmlString">personsTree</span>&#8220;</p>
<div class="indent4">
	dataProvider=&#8221;<span class="mxmlString">{people}</span>&#8220;<br />
	labelField=&#8221;<span class="mxmlString">name</span>&#8220;<br />
	width=&#8221;<span class="mxmlString">100%</span>&#8220;<br />
	height=&#8221;<span class="mxmlString">100%</span>&#8221; <span class="mxmlComponent">/&gt;</span></div>
<p>	<span class="mxmlComponent">&lt;mx:HBox&gt;</span></p>
<div class="indent">
	<span class="mxmlComponent">&lt;mx:Label</span> text=&#8221;<span class="mxmlString">Filter the Tree:</span>&#8221; <span class="mxmlComponent">/&gt;</span><br />
    <span class="mxmlComponent">&lt;mx:TextInput</span> id=&#8221;<span class="mxmlString">iNameFilter</span>&#8221; change=&#8221;<span class="mxmlString">refreshData()</span>&#8221; <span class="mxmlComponent">/&gt;</span></div>
<p>	<span class="mxmlComponent">&lt;/mx:HBox&gt;</span></div>
<p>    <span class="mxmlComponent">&lt;/mx:VBox&gt;<br />
    </span>
	</div>
<p>        <span class="mxmlComponent">&lt;/mx:Application&gt;</span>
	</div>
<div style="text-align: center;" mce_style="text-align: center">
<p>You may right-click and select &#8220;View Source&#8221; <br />
to view the full source code for the following example.<br />
<iframe id="filteringTreeFrame" src="/assets/pages/treefiltering.html" mce_src="/assets/pages/treefiltering.html" scrolling="no" width="400" frameborder="0" height="560"></iframe>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.kalengibbons.com/blog/index.php/2009/01/filtering-a-flex-tree-using-an-arraycollection/feed/</wfw:commentRss>
		<slash:comments>53</slash:comments>
		</item>
	</channel>
</rss>

