If you have ever wanted to know how to set the label of a tree node to
a different value you've found the right tutorial. To begin follow the steps in the tutorial "Adding a tree component
to the stage" and continue here.
There are two ways to set the label of a node. You can define
an label field or create an label handler function. We will go over each one below.
Defining the Label Field
There
is a tree component property called "labelField" that defines the
attribute that contains the path to the image to use as your label. The
tree component looks for a "label" attribute in your node. If it does
not find it your tree node displays this message
"[type Function], [type Function]". If we know the name of the
attribute that will describe our node then you can tell the labelField
property and it will use that attribute instead of the default "label"
attribute.
This
allows you to use any attribute defined in your xmlnode to generate the
label. But if you want more control
then you can use the label handler.
Creating an Label Handler
You
can also create an label handler that allows you to handle more complex
scenarios. You could use this to show a default label for nodes that do
not have a label field attribute defined as above or join two
attributes together to create a custom label. The first example joins
two attributes values together to create a custom label.
The next example checks if a attribute exists. If it does not exist it
uses the node name and displays it instead. If neither exist then it
displays a very original label, "No Label".
In both examples we can access the current node in the "item"
reference. We access the node attributes through the "attributes"
array. We can check if an attribute exists by checking if the value is
undefined. Both examples return a string to display as a label but we
don't
need to return anything if we want to use the value of the default
label attribute.
Modifying the XML
Test your Movie
Test your movie.