I've used something like this (which is possibly a bit flaky, I admit. I wrote this a long time ago and haven't revisited it since).
private function displayTreeNode(node:XML):void
{
expandTreeParents(node);
movieTree.selectedItem = node;
movieTree.expandItem(node,true,false);
var idx:int = movieTree.getItemIndex(node);
movieTree.scrollToIndex(idx);
movieTree.firstVisibleItem = node;
}
private function expandTreeParents(node:XML):void {
if (node && !movieTree.isItemOpen(node)) {
movieTree.expandItem(node, true);
expandTreeParents(node.parent());
}
}
where movieTree is my tree instance.
You need to traverse up the tree from the node you want to display and open
each item ( expandTreeParents() above ),
then select the node you want to display. This may not be visible even then,
hence the scroll to index stuff.
Hope that's enough to get you going, in any case.
-----Original Message-----
From: [email protected] on behalf of thedauntless_ff
Sent: Wed 12/30/2009 1:31 PM
To: [email protected]
Subject: [flexcoders] Selecting xml nodes in a tree
Hi,
My situation:
- 1 Tree component (id=tree)
- 1 XMLListCollection as a dataprovider for the tree (id=treeData)
- 1 String, a url, that has to be found & selected.
My XMLListCollection looks like this:
<page title="page1" href="url1">
<page title="subpage1" href="url2"/>
<page title="subpage2" href="url3">
<page title="subsubpage1" href="url4">
</page>
</page>
<page title="page2" href="url5">
<page .../>
<page .../>
</page>
Etc. All nodes are named 'page' and have a href property.
What I need now, is a way to select the node in the tree for the given url. For
example, all nodes are collapsed and I want to select the node with href=url4.
How can I do this?
This is what I tried:
var t:XMLListCollection = this.treeData;
var url:String = "myURL";
for(var i:Number = 0; i<t.length; i++)
{
//this works, nodes now contains a list of xml items with the correct
url.
var nodes:XMLList = t[i]..page.(@href==url);
if(nodes.length() > 0)
{
//I only want to select the first occurence
tree.selectedItem = nodes[0];
break;
}
}
This, however, gives me a weird error (probably not that helpfull):
TypeError: Error #1010: A term is undefined and has no properties.
at
mx.controls.listClasses::ListBase/setSelectionDataLoop()[E:\dev\beta1\frameworks\projects\framework\src\mx\controls\listClasses\ListBase.as:7360]
at
mx.controls.listClasses::ListBase/commitSelectedItems()[E:\dev\beta1\frameworks\projects\framework\src\mx\controls\listClasses\ListBase.as:7251]
at
mx.controls.listClasses::ListBase/commitSelectedItem()[E:\dev\beta1\frameworks\projects\framework\src\mx\controls\listClasses\ListBase.as:7216]
at mx.controls.listClasses::ListBase/set
selectedItem()[E:\dev\beta1\frameworks\projects\framework\src\mx\controls\listClasses\ListBase.as:3474]
at classes::Logic/onBrowserLocationChange()[C:\Users\ser\Documents\Flex
Builder 3\application\src\classes\Logic.as:346]
at flash.events::EventDispatcher/dispatchEventFunction()
at flash.events::EventDispatcher/dispatchEvent()
at
mx.core::UIComponent/dispatchEvent()[E:\dev\beta1\frameworks\projects\framework\src\mx\core\UIComponent.as:11260]
at
Classes::Logic/onBrowserLocationChange()[C:\Users\user\Documents\Flex Builder
3\application\src\classes\Logic.as:73]
at flash.events::EventDispatcher/dispatchEventFunction()
at flash.events::EventDispatcher/dispatchEvent()
at
mx.core::UIComponent/dispatchEvent()[E:\dev\beta1\frameworks\projects\framework\src\mx\core\UIComponent.as:11260]
at
mx.controls::HTML/htmlLoader_locationChangeHandler()[E:\dev\beta1\frameworks\projects\airframework\src\mx\controls\HTML.as:1438]
at flash.events::EventDispatcher/dispatchEventFunction()
at flash.events::EventDispatcher/dispatchEvent()
at flash.html::HTMLLoader/onLocationChangeTimer()
at flash.utils::Timer/_timerDispatch()
at flash.utils::Timer/tick()
Greets,
Dauntless
______________________________________________________________________
This communication is from Primal Pictures Ltd., a company registered in
England and Wales with registration No. 02622298 and registered office: 4th
Floor, Tennyson House, 159-165 Great Portland Street, London, W1W 5PA, UK. VAT
registration No. 648874577.
This e-mail is confidential and may be privileged. It may be read, copied and
used only by the intended recipient. If you have received it in error, please
contact the sender immediately by return e-mail or by telephoning +44(0)20 7637
1010. Please then delete the e-mail and do not disclose its contents to any
person.
This email has been scanned for Primal Pictures by the MessageLabs Email
Security System.
______________________________________________________________________<<winmail.dat>>

