So far we have only added blank nodes. How do we store data in nodes? The
JNode class is a subclass of JObject; this means that we have access to the get() and
set() methods:
$node = new JNode();
$node->set('name', 'Child Node 1');
$tree->addChild($node);
Chapter 12
[ 361 ]
Although this makes the JNode class more useful, we can make nodes that are
designed especially for our needs. The best way for us to make use of the JTree is to
define a new JNode subclass that has additional properties:
// subclass of JNode
class myNode extends JNode
{
// name property
var $name = '';
// constructor
function __construct($name='')
{
$this->set('name', $name);
parent::__construct();
}
}
Now we can create a far more bespoke tree:
$tree->addChild(new myNode('Node 1'));
$tree->addChild(new myNode('Node 2'));
A prime example to this sort of use of the JTree class is the iLink and iLinkNode
classes. These two classes extend the JTree and JNode classes respectively. They are
used to build the menu trees that are commonly used in Joomla!.
Pages:
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503