Trees are used to model hierarchical data. Joomla! provides us with the JTree
and JNode
classes; we can use these to build tree data structures. Before we start using these classes we must import the relevant library:
The first thing we do when building a new tree is to create a new JTree
object. Although a JTree
object is not technically required in order to create a tree it ensures we can easily access the root of the tree. There are no parameters that we need to pass when creating a new tree:
$tree = new JTree();
When a new tree is created a new root JNode
object is automatically created. The root node is the node to which all other nodes in the tree belong.
Once we have created a tree we must add child nodes by using the addChild()
method:
$tree->addChild(new JNode());
When we use the JTree
method addChild()
, the child isn't necessarily added as a direct descendant to the root node. Trees use a pointer to determine the current or working node. When we...