Pages
Categories
Tags
Abstraction Ant Apache Autocomplete CFCUnit CodexWiki ColdFusion Components Constants CSS DAO Dates Dependency Injection Design Patterns Eclipse File Flash Forms Frameworks Gateways Groovy Guice Hosting HTML IIS Java jQuery Mappings MSSQL MySQL Nested Set Trees OOP pagination Railo Security SES URLs Spring Strategy Subversion Timestamp Tomcat Tools Ubuntu Unit Testing ValidationArchives
Nested Set Tree Objects
The NST library objects
There are three NST library objects:
NestedSetTreeTable
The first object you need to create is the NestedSetTreeTable object. You must create one for each table that will hold nested set data. These may be stored in the application scope.
The object requires the following parameters when created:
Example of creating the nested set tree table object:
Or you can use the simpler version if you use the default column names:
The NestedSetTreeTable has the following functions available:
NestedSetTree
The main object you need to use is the NestedSetTree. This object contains all of the functions you need to manipulate you tree. You create NestedSetTree object from the NestedSetTreeTable:
Or if your table is only expected to contain one tree:
<cfset variables.nstree = variables.nstTable.getNestedSetTree()>The following examples will use a simple private function nst() as an alias to the NestedSetTree object:
Node
The Node object is created whenever you read a record from the NestedSetTree. For example:
<cfset node = nst().getNode(10)>A Node has many functions available, the basic node oriented functions are:
Creating the root node of a new tree
Nested set trees need to have a top level root node which is the parent of all other nodes. When you have no nodes in your tree, then you can use the following code to create your first record.
If you are converting an existing data table to use the NST model, then see “Creating a NST for existing data” below.
To create a new root node in the NST, use the NestedSetTree newRoot() function
Example code:
Retrieving nodes from the tree
The NestedSetTree object provides the following functions to retrieve nodes.
These functions always return a node. When you have the result node, you should test if the node is valid.
The Node object has the following functions to retrieve nodes.
Testing for a valid node
Use isValidNode() to test if a node is valid. An invalid node is essentially equivalent to receiving a null or undefined value.
On a NestedSetTree use:
On a Node use:
Querying for nodes in the tree
The following functions return a true or false value.
For the NestedSetTree:
For the Node use:
Adding new nodes
The following functions are provided to add nodes to a tree.
For Node objects use:
Example code to add new children
Deleting nodes
The NestedSetTree object provides the following delete functions:
The Node object provides just a single function:
Example code to delete a node:
Moving nodes
The following functions are available to move nodes within your tree:
And for the Node:
Note that in these examples, the sort order of the nodes is managed by the NST rather than the primary data table (via a ‘sort’ column)
so we will only need to update the NST table.
Moving a node ‘up’
Example code to move a node up/left within the same parent.
Moving a node ‘down’
Example code to move a node down/right within the same parent.
Moving a node to another branch in the tree
Note that nodes may only be moved to either parent or sibling nodes, so we need to ensure the move is permitted.
Querying the data
The library provides some functions to query the NST table:
And for the Node:
However, when querying the data you mostly want to also obtain data from your primary table:
Getting the complete tree hierarchy
The ‘left’ column in the NST provides all we need to get a complete hierarchy of the tree.
It is often useful to get the depth level of the nodes at the same time.
Getting the level of a node
First let’s look at getting the depth of a single node.
Getting the complete tree hierarchy with depth levels
Now we can combine the complete tree hierarchy and level queries together. The level query is included as a subquery.
Getting a subtree with depth levels
If you only need to get a subtree (rather than the complete tree) with levels, then you may use code such as:
Getting a breadcrumb record set
Use the following to get a query of the breadcrumb links for a node.
Rendering nested set output as an unordered list
You can display a nested set query’s output as nested unordered lists using the following code.
First you would need to execute a query that returned either the entire tree or a subtree plus corresponding depth levels (as described above).
In this example, the “treeQuery” query contains a “level” column and a “categoryName” column.
Creating a NST for existing data
If you have existing data that you want to convert to use the NST model, then the library provides a rebuildIndex() function to help you with this conversion:
This function has four parameters:
When run, all records in the NST table will be deleted and recreated.
Example usage: