tevin
10-05-2007, 11:12 AM
Hi,
I have a custom component that, when clicked, calls a webservice which returns an XML hierarchy. I set this hierarchy to be the dataProvider for a Tree component, which is then added to the childlist. I am searching the XML for a node which matches a specific id, and setting this node to be my Tree's selectedItem. It is finding my requested node ok, but I can't get the Tree to expand this node's parent nodes so that the selectedItem is visible. Here's my code:
private var myTree:Tree;
private var buttonText:TextField;
private var myWebservice:WebService;
//ctor
public function myComponent()
{
buttonText = new TextField();
buttonText.addEventListener(MouseEvent.CLICK, setupTree);
// Setup state change
var treeShown:State = new State();
treeShown.name = "tree_shown";
var addTree:AddChild = new AddChild(this, myTree);
treeShown.overrides[0] = addTree;
states.push(treeShown);
}
// Calls webservice to build hierarchy
private function setupTree(event:MouseEvent):void
{
myWebservice.build_hierarchy.addEventListener(Resu ltEvent.RESULT, onHierarchy);
myWebservice.build_hierarchy.send();
}
// Populates tree and sets selectedItem
private function onHierarchy(event:ResultEvent):void
{
if (event.result != null) {
myTree.dataProvider = new XML(event.result).node;
myTree.labelField = "@label";
// Searches all descendants for matching attribute
var selectedId:String = "requested_node";
var selectedNodes:XMLList = XML(myTree.dataProvider)..node.(@id==selectedId);
// Expands all parents above the matching node and sets that node to be our selectedItem
if (selectedNodes[0] != null) {
expand_parents(selectedNodes[0]);
myTree.selectedItem = selectedNodes[0];
}
currentState = "tree_shown";
}
}
// Expands each parent above the selected node
private function expand_parents(node:XML):void
{
// Validates tree so that expandItem() works...
myTree.validateNow();
while (node.parent() != null) {
node = node.parent();
myTree.expandItem(node, true);
}
}
My XML looks something like:
<nodes>
<node label="World" id="TOT">
<node label="Europe" id="EUR">
<node label="UK" id="UK"/>
<node label="Germany" id="GER"/>
<node label="France" id="FRA"/>
</node>
</node>
</nodes>
Any help would be appreciated.
I have a custom component that, when clicked, calls a webservice which returns an XML hierarchy. I set this hierarchy to be the dataProvider for a Tree component, which is then added to the childlist. I am searching the XML for a node which matches a specific id, and setting this node to be my Tree's selectedItem. It is finding my requested node ok, but I can't get the Tree to expand this node's parent nodes so that the selectedItem is visible. Here's my code:
private var myTree:Tree;
private var buttonText:TextField;
private var myWebservice:WebService;
//ctor
public function myComponent()
{
buttonText = new TextField();
buttonText.addEventListener(MouseEvent.CLICK, setupTree);
// Setup state change
var treeShown:State = new State();
treeShown.name = "tree_shown";
var addTree:AddChild = new AddChild(this, myTree);
treeShown.overrides[0] = addTree;
states.push(treeShown);
}
// Calls webservice to build hierarchy
private function setupTree(event:MouseEvent):void
{
myWebservice.build_hierarchy.addEventListener(Resu ltEvent.RESULT, onHierarchy);
myWebservice.build_hierarchy.send();
}
// Populates tree and sets selectedItem
private function onHierarchy(event:ResultEvent):void
{
if (event.result != null) {
myTree.dataProvider = new XML(event.result).node;
myTree.labelField = "@label";
// Searches all descendants for matching attribute
var selectedId:String = "requested_node";
var selectedNodes:XMLList = XML(myTree.dataProvider)..node.(@id==selectedId);
// Expands all parents above the matching node and sets that node to be our selectedItem
if (selectedNodes[0] != null) {
expand_parents(selectedNodes[0]);
myTree.selectedItem = selectedNodes[0];
}
currentState = "tree_shown";
}
}
// Expands each parent above the selected node
private function expand_parents(node:XML):void
{
// Validates tree so that expandItem() works...
myTree.validateNow();
while (node.parent() != null) {
node = node.parent();
myTree.expandItem(node, true);
}
}
My XML looks something like:
<nodes>
<node label="World" id="TOT">
<node label="Europe" id="EUR">
<node label="UK" id="UK"/>
<node label="Germany" id="GER"/>
<node label="France" id="FRA"/>
</node>
</node>
</nodes>
Any help would be appreciated.