Creating dynamic tree structure menu systems
- Introduction
- A basic example
- The constructor function properties
- Node properties
- The short form of node configuration
- Customising the appearance with CSS
Introduction
The RGraph Tree object that was bundled with version 7.01 has a very useful capability that allows you to produce dynamic (or static) tree menu structures like the one shown here on the right. These have many uses - one of which could be showing the various options that are available to users of your online management system (instead of using a flat list of links.
The menu system is easy to configure and can be customised to fit your requirements. The code for a basic menu is shown below.
A basic example
<script src="RGraph.common.core.js" ></script> <div id="myTree"></div> <script> // // Create the tree structure object. The text that's // provided here becomes the root node. // structure = new RGraph.Tree('My treemenu'); // // Add three first level nodes. Objects are used as // the arguments to the add function which contain // both the text label and the link that's used by // the node. // structure.add({text: 'Option one',link:'/option1/index.html'}); structure.add({text: 'Option two',link:'/option2/index.html'}); structure.add({text: 'Option three',link:'/option3/index.html'}); // // Add another node but assign the result to a // variable. This is so that sub-nodes can then // be added to it. // option4 = structure.add('Option four'); // // Now add the sub-nodes to the "Option 4" node. // option4.add({text: 'Sub-option1',link:'/option4/suboption1/index.html'}); option4.add({text: 'Sub-option2',link:'/option4/suboption2/index.html'}); option4.add({text: 'Sub-option3',link:'/option4/suboption3/index.html'}); // // Now create the object that converts the menu // to a dynamic tree menu. The options shown below // can be used with this object in the options block. // new RGraph.Treemenu({ id: 'myTree', data: structure, options: { } }).draw(); </script>
Pretty easy to configure as you can see and there's more options, detailed below, that are available to both the constructor function and also the tree nodes when you create them.
The constructor function options
These properties can be used in the constructor function and apply to the whole tree.
new RGraph.Treemenu ({
id: 'myTree',
data: structure,
options: {
// These can be paths to images on your server or data: URLs
images: {
folder: '...',
folderexp: '...',
page: '...',
line: '...',
branch: '...',
branchtop: '...',
branchbottom: '...',
plus: '...',
plusbottom: '...',
plustop: '...',
minus: '...',
minusbottom: '...',
minustop: '...'
}
}
}).draw();
Any of these images can also be set to none if you didn't want an image so you could do this:
<script> new RGraph.Treemenu ({ id: 'myTree', data: structure, options: { images: { branchtop: 'none', branchbottom: 'none', branch: 'none', line: 'none', plustop: 'none', plusbottom: 'none', plus: 'none', minustop: 'none', minusbottom: 'none', minus: 'none' } } }).draw(); </script>
Node properties
These properties can be used with the add() function when you add nodes to the tree and apply to that individual node.// ... style: [ 'div.myNode img:last-of-type {opacity: 0.5;}' ] // ...


The short form of node configuration
If your needs just consist of specifying the link and the text for a node then instead of just the text you can give both the text and a link separated by a colon instead of a whole object. This looks like this:
<script> structure = new RGraph.Tree('People'); structure.add('Richard:https://www.richard.com'); structure.add('Gabriel:https://www.gabriel.com'); structure.add('Charles:https://www.charles.com'); structure.add('Tommy:https://www.tommy.com'); new RGraph.Treemenu({ id: 'myTree', data: structure, options: { } }).draw(); </script>
Customising the appearance with CSS
In a similar way to the datagrid component, the tree menu visualisation can be customised by using the style property. This is an easy way of adding CSS that's applied to the tree menu instead of having it in a separate CSS file or in a separate style block in the page. An example of using it is:
<script> new RGraph.Treemenu({ id: 'myTree', data: structure, options: { style: [ /* Set the dimensions of the icon that we've provided */ 'div#myTree div img:last-of-type {width: 20px; height: 20px;}', /* Set the size of the text (that ISN'T a link) */ 'div#myTree div span.rgraph-tree-branch-label {font-size: 20pt;}', /* Set the size of the text (that IS a link) */ 'div#myTree div span.rgraph-tree-branch-label a {font-size: 20pt;}' ] } }).draw(); </script>
The div#myTree bit at the start of each rule isn't strictly necessary as if it's not there it will be added to restrict the CSS rule to this tree menu. This means that to apply something to the whole tree menu you can do this:
<script> new RGraph.Treemenu({ id: 'myTree', data: structure, options: { style: [ '{font-size: 20pt;}' ] } }).draw(); </script>
Or this (they're the same)
<script> new RGraph.Treemenu({ id: 'myTree', data: structure, options: { style: [ 'div#myTree {font-size: 20pt;}' ] } }).draw(); </script>