I fine-tuned it to the following (some lines are commented, just to give a
complete example for future google-visitors)
private function displayTreeNode(node:XML):void
{
var t:Number = getTimer();
//expand all the parents
expandToTop(node.parent());
//item is now selectable
tree.selectedItem = node;
//open the item itself
//tree.expandItem(node,true,false);
//make the node visible (=position scrollbar)
var idx:int = tree.getItemIndex(node);
tree.scrollToIndex(idx);
//tree.firstVisibleItem = node;
trace("Looking up the item in the tree took " + (getTimer() - t)
+ "ms");
}
private function expandToTop(node:XML):void
{
//open if it's not open yet
//if(!tree.isItemOpen(node)) tree.expandItem(node, true);
tree.expandItem(node, true);
//if there's a parent, continue recursion
if(node.parent()) expandToTop(node.parent());
}
The only problem now is that my treeData is really big and the E4X statement
takes about 1200ms on average. Updating the tree takes up another 800ms.
I'll have to optimize the crap out of this :).
On Wed, Dec 30, 2009 at 5:21 PM, Jim Hayes wrote:
>
>
> It strikes me now that if it meets an open node on the way up the tree it
> will stop there (and probably shouldn't)
> You might want to allow for that / fix it.
> It's something that I put in as a feature request that was subsequently
> depreciated because when the client saw it in practice they decided they
> didn't like it after all!
> So it's not all that well tested I'm afraid.
>
>
> -Original Message-
> From: flexcoders@yahoogroups.com on behalf
> of Jeroen Beckers
> Sent: Wed 12/30/2009 3:20 PM
> To: flexcoders@yahoogroups.com
> Subject: Re: [flexcoders] Selecting xml nodes in a tree
>
> Hi Jim,
>
> I think that'll be enough to get me going, thanks! :).
>
> And if not, you'll hear from me soon enough :).
>
> Greets,
> Dauntless
> On Wed, Dec 30, 2009 at 3:36 PM, Jim Hayes
> >
> wrote:
>
> >
> >
> > 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: flexcoders@yahoogroups.com 40yahoogroups.com> on behalf
> > of thedauntless_ff
> > Sent: Wed 12/30/2009 1:31 PM
> > To: flexcoders@yahoogroups.com 40yahoogroups.com>
> > 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:
> >
> >
> >
> >
> >
> >
> >
> >
> >
> >
> >
> > 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 > {
> > //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