0

How do I indicate the visit history on a flex tree component? I want to highlight the clicked/visited nodes to a different color and will not change after that, so that all the visited nodes will be one color.

I tried adding an attribute to the underlying XML by

var selected:XML=app.treeObj.selectedItem as XML;
if(!selected.hasOwnProperty("visited"))
{
    selected.@visited = "true";

}

and have an itemrenderer for the tree as below.

public class CustomTreeItemRenderer extends TreeItemRenderer
    {
        public function CustomTreeItemRenderer()
        {
            super();
        }

        override public function set data(value:Object):void
        {
            if(value !=null)
            {

                super.data = value;
                if(value.@visited=="true")
                {
                    setStyle("color", 0x000000);
                }

                invalidateDisplayList()

            }
        }

    }

This code does retain the new color, but it also changes the color of nodes which are not visited at all. What am I doing wrong here? Is there any better way to achieve this?

Vipin

Amarghosh
  • 58,710
  • 11
  • 92
  • 121
Vipin
  • 11
  • 3
  • Just to clarify: are you calling `selected.@visited = "true";` from the select-handler of the tree or even before assigning it to the tree's `dataProvider`? – Amarghosh Oct 26 '09 at 04:37
  • I am calling it on the selectedItem on itemClickHandler.. – Vipin Oct 26 '09 at 15:42

1 Answers1

2

In your set data, you need to set the style back to the original one if it's not been visited, otherwise when the renderer is recycled to an unvisited node, it retains the visited colour.

Try...

override public function set data(value:Object):void
    {
            if(value !=null)
            {

                    super.data = value;
                    if(value.@visited=="true")
                    {
                            setStyle("color", 0x000000);
                    } 
                    else 
                    {
                            setStyle("color", originalColor);
                    }

                    invalidateDisplayList()

            }
    }

Not the most efficient way of doing it (you'll be setting the color even if you don't need to, and setStyle is an expensive operation), but it'll work.

Gregor Kiddie
  • 3,119
  • 1
  • 19
  • 16
  • Thanks Gregor..I was so close to it.. Thanks for your timely response.. and it works now. Thanks again. – Vipin Oct 26 '09 at 15:41