Initialize object on page
Parameters passed to the constructor are:
object to attach tree to (should be loaded before calling constructor)
width of the tree
height of the tree
identifier of level parent to tree root level (superroot)
Specify Additional parameters of the tree:
setImagePath(url) - method specifies the path to the folder with tree icons
enableCheckBoxes(mode) - enable/disable checkboxes in tree (checkboxes appear by default)
enableDragAndDrop(mode) - enable/disable drag-n-drop in tree
Set Event Handlers
In most cases functions specified inside event handlers get some values with the arguments. For details about passed arguments please refer to
API documentation.
Adding nodes with Script
0s passed to the function for arguments 4-7 (function to call on select, images) mean use default values for them
Last argument is a comma delimited string of following possible value (upper case only):
SELECT - move selection to this node after inserting
CALL - call function on select
TOP - add node to the top position
CHILD - node has children
CHECKED - checkbox is checked (if exists)
Loading data with XML
ID of opened node (as id url parameter) will be added to URL specified in initXMLAutoLoading(url) during the call
No additional ID is added to the url specified in loadXML(url) method during the call
Using loadXML() without parameters you load url specified in initXMLAutoLoading(url) method
XML Syntax:
-
true
<tree> node is mandatory. It specifies the parent of loading block of data. According to this its id parameter specifies the ID oth that parent.
To load root level you need to specify the ID you used when created tree object: new myObjTree(boxObject,width,height,0)
<item> can contain subitems (in order to load more than one level at once) or not.
Mandatory parameters for this tag are:
text - label of the node
id - id of the node
Optional parameters for this tag are:
im0 - image for node without children (tree will get images from the path specified in setImagePath(url) method)
im1 - image for opened node with children
im2 - image for closed node with children
acolor - colour of not selected item
scolor - colour of selected item
select - select node on load (any value)
open - show node opened (any value)
call - call function on select(any value)
checked - check checkbox if exists(any value)
child - spec. if node has children (1) or not (0)
imheight - height of the icon
imwidth - width of the icon
To set userdata directly within XML use <userdata>
It has just one parameter:
name
and value to specify userdata value
Setting custom icons to nodes
There are two ways to set custom icons for the node. And it depends on the way you add items.
Important: tree will get images for the nodes from the path specified in setImagePath(url) method.
Javascript way. Using arguments of insertNewChild(...) or insertNewNext(...) methods:
XML way. Using parameters of <item> tag:
-
im0 - image for node without children (tree will get images from the path specified in setImagePath(url) method)
im1 - image for opened node with children
im2 - image for closed node with children
Building dynamic tree
If your tree should contain large amount of nodes (or you simply do not want to waist time loading hidden nodes), it would be better to load them on demand, not at once.
For this purpose we made the tree to load levels dynamically using XML.
See chapter "Loading data with XML"
Manipulating nodes
A few examples of manipulating node with tree object methods:
Serializing Tree
Serialization methods allows getting tree in xml representation (xml string). Various levels of serializations define the number of tree parameters reflected in resulting XML:
no parameters - id,open,select,text,child
userDataFl true - userdata
itemDetailsFl true - im0,im1,im2,acolor,scolor,checked,open
Moving Nodes
To move Item programaticaly developer can use the folowing method:
Another way is to use doCut(),doPaste(id) - but this works with selected item only. Developer can also delete node in one place and create it in other (also the way ;-)).
To give users the possibility to move items use drag-n-drop functionlity.
Items Counter
There is a possibility to display item children elements counter with the label of the node. To activate this feature use the following:
Possible modes are:
"child" - all childs on this level
"leafs" - all childs without subchilds on this level
"childrec" - all childs
"leafsrec" all childs without subchilds
"disabled" - nothing
Other related methods:
_getChildCounterValue(itemId) - get current counter value
setChildCalcHTML(before,after) - html around counter to change default [x]
Use child attribute in xml if you need to set counter value when using dynamical loading.
Smart XML Parsing
The idea of Smart XML Parsing is simple - the entire tree structute loaded on client side, but only nodes which should be displayed are rendered. This helps to dramaticaly decrease loading time and general performance of large trees. Plus - in the contrary to Dynamical Loading - entrire tree structure is available for most of script methods (for example Search performed agains all nodes - not only rendered). To activate Smart XML Parsing use the following method:
Smart XML Parsing do not work if tree loaded fully expanded.
Checkboxes in tree
dhtmlxTree support two or three state checkboxes. Three state checkboxes means: checked/unchecked/some-child-checked (not all).
To enable three state checkboxes use the following method:
Using three-state checboxes with smart xml parsing you need to specify third state manually (checked="-1"):
-
Checkboxes can be disabled - disableCheckbox(id,state)
Some node can hide checkboxes - showItemCheckbox(id,state)
Drag-and-drop technics
There are three modes of drag-n-drop:
Drop as child - "child"
Drop as sibling - "sibling"
Complex mode (both previous are active) - "complex"
Plus two modes of each of them:
1. Common drag-n-drop
2. Copy with drag-n-drop - tree.enableMercyDrag(1/0)
All modes can be changed on the fly.
Event handlers
To process drag-n-drop before drop occured use onDrug event handler - setDragHandler(func). If func doesn't return true, drop will be canceled.
After drop occured there is another event - onDrop - handle it using setDropHandler(func).
Both event handlers pass 5 parameters into the func object:
id of node which was dragged
id of target node
id of target-before node (if drop as sibling)
tree object source
tree object target
Increasing Performance
Taking into account the general low performance of DHTML we introducrd two ways of increasing performance of large trees:
1.
Dynamical Loading
2.
Smart XML Parsing
Also be sure your tree was organized well - putting a lot of items on one level of the branch makes tree difficalt for visual perception and decrease performance.
Context Menu
There is built in context menu in dhtmlxTree. The content of this menu can be set with XML or script. For changing context menu content depending on tree item developer can implement function for hidding/showing items of the same menu or associate different menues with different items. To enable context menu do the following:
Refreshing nodes
...
Icons in Tree
Set Icons
There is a way to set icons for the node explicetly using script (setItemImage,setItemImage2) or xml (im0,im1,im2 attributes of item node):
im0 - item without children
im1 - closed item with children
im2 - opened item with children
Set Icon Size
There is a way to set icon size for the whole tree or each node using script or xml:
XML syntax for icon size per each item (optional):
Script syntax:
tree.setIconSize(w,h);//set global icon size
tree.setIconSize(w,h,itemId)//set icon size for particular item