I am facing a problem while updating dataprovider for tree control.
DataProvider is dependent on two different data sources..... Using which I
am building new collection which acts as a dataprovider to tree control.
I want to add/update/delete data in either one or both data sources. It
should get reflected in tree control. But I am facing problem while doing
so....
I have expended nodes in tree and then if i try to update data source, tree
control does reflect the changes but it closes all expanded nodes.
Following is the sample prototype:
<s:Application
xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/halo"
initialize="initTree(event)">
<fx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
import mx.events.CollectionEvent;
import mx.events.FlexEvent;
[Bindable]
private var treeData : ArrayCollection = new
ArrayCollection();
[Bindable]
private var testData : ArrayCollection;
[Bindable]
private var testData2 : ArrayCollection;
private var openItems : Object;
private var refresh : Boolean = false;
//-----------------------------------------------------------
protected function initTree(event:FlexEvent):void
{
treeData.addEventListener(CollectionEvent.COLLECTION_CHANGE, update);
testData = new ArrayCollection([{label:"One"},
{label:"Two"},
{label:"Three"}]);
testData2 = new ArrayCollection([{label:"One"},
{label:"Two"},
{label:"Three"}]);
updateData();
}
//-----------------------------------------------------------
protected function button_clickHandler(event :
MouseEvent):void
{
openItems = treeControl.openItems;
testData.addItem({label:"updating"});
updateData();
}
//-----------------------------------------------------------
// Build data provider as per requirement
private function updateData() : void {
var tree : ArrayCollection = new
ArrayCollection();
for(var i : int = 0; i < testData.length; i++) {
var col : ArrayCollection = new
ArrayCollection();
for(var j : int = 0; j <
testData2.length; j++) {
if(testData.getItemAt(i).label
== testData2.getItemAt(j).label) {
col.addItem({label:testData2.getItemAt(j).label});
}
}
tree.addItem({label:testData.getItemAt(i).label, children:col});
}
treeData = tree;
}
//-----------------------------------------------------------
private function update() : void {
refresh = true;
}
//-----------------------------------------------------------
protected function
treeControl_renderHandler(event:Event):void
{
if(refresh) {
treeControl.invalidateList();
refresh = false;
treeControl.openItems = openItems;
treeControl.validateNow();
}
}
]]>
</fx:Script>
<mx:VBox width="100%">
<s:Button id="button" click="button_clickHandler(event)"
label="Click"/>
<mx:Tree id="treeControl" width="100%" right="-1" left="-1"
bottom="-1"
top="44" borderAlpha="0.75" dataProvider="{treeData}"
render="treeControl_renderHandler(event)"/>
</mx:VBox>
</s:Application>
-----
--
Regards,
Yogesh Patil.
--
View this message in context:
http://old.nabble.com/Problem-while-updating-tree-control-tp27652144p27652144.html
Sent from the FlexCoders mailing list archive at Nabble.com.