- Menu scripts
- Window scripts
- Calendar scripts
- Gallery scripts
- Form widgets
- Tab view scripts
- Table widgets
- Drag and drop
- Folder trees
- Tooltips
- AJAX scripts
- Content Effects

- Misc scripts
- Javascript games
- Chess widgets
- DHTML Suite
- Resources
- cbolson.com
Download drag'n drop folder tree
Licensing
This script is distributed under the LGPL open source license.
Commercial licenses are also available. Some of these licenses also includes personal e-mail support for up to 1 year.
Download files
You can download the entire script from this Zip file.
Files included in the package:
- drag-drop-folder-tree.html = Main html file
- js/drag-drop-folder-tree.js = Main Javascript file
- js/context-menu.js = JS code for the context menu
- js/ajax.js = Ajax (Library from http://twilightuniverse.com/projects/sack). Used for the save method.
- saveNodes.php = Saving nodes. You need to configure this file, i.e. database connection and query.
- folderTree_updateItem.php = Rename or delete an item on the server. This file is called by ajax.
- images/* = Images used by the script
- css/drag-drop-folder-tree.css = CSS file used by the script
- css/context-menu.css = CSS file used by the context-menu
Configuration
Define the tree
The tree is created by a standard <UL><LI> list as in drag-drop-foler-tree.html.
Notice that you have three attributes available which you could assign to your <LI> tags.
- noDrag - If you set this to "true", it means that this list item won't be dragable.
- noChildren - When this attribute is set to true, it won't be possible to move items to this node.
- noSiblings = It is not possible to add items above or below this node, only children.(This is an attribute you typically use only on a root node).
- noDelete = It is not possible to delete this node.
- noRename = It is not possible to rename this node.
Creating tree object
Below the HTML of your tree, you create a tree object like this:
<script type="text/javascript">
treeObj = new JSDragDropTree();
treeObj.setTreeId('dhtmlgoodies_tree2');
treeObj.setMaximumDepth(5);
treeObj.setMessageMaximumDepthReached('Maximum depth reached');
treeObj.initTree();
</script>
First, you create the tree object, then you define the id of your tree, i.e. the id of the top <UL>(example: <ul id="dhtmlgoodies_tree2"...).
The following methods until initTree() are optional. See description of available methods below.
Methods
the JSDragDropTree class has the following methods:
- setMaximumDepth = Setting maximum depth of tree. Dropping of nodes would be cancelled automatically if the depth exceeds this number. Instead it would move the dragged nodes back to it's original location. Default value is 6.
- setMessageMaximumDepthReached = Message to the user when he drop nodes and depth exceeds defined maximum depth.
- setImageFolder = Specify path to images. Default is "images/".
- setFolderImage = Specify name of folder image, example: "dhtmlgoodies_folder.gif"
- setPlusImage = Specify name of small [+] image
- setMinusImage = Specify name of small [-] image
- setTreeId = Specify id of tree
- expandAll = Expand all nodes - must be called after initTree()
- collapseAll = Collapse all nodes - must be called after initTree()
- getNodeOrders = Returns the order of the nodes in the tree in the format id-parentID,id-parentID,id-parentID
- setFileNameRename = Specify name of file to call after a node has been renamed. Variables to this file will be "renameId" and "newName". Default file is "folderTree_updateItem.php"
- setFileNameDelete = Specify name of file to call after a node has been deleted. Variables to this file will be "deleteIds" which is a commaseparated list of ids for the deleted nodes. Default file is "folderTree_updateItem.php"
- setRenameAllowed = Enable or disable rename of nodes. This variable is default set to true. Set it to false if you don't want to allow any nodes to be renamed.
- setDeleteAllowed = Specify if it's possible to delete nodes in the tree. This variable is default set to true. Set it to false if you don't want to allow any nodes to be deleted.
- additionalRenameRequestParameters = Add parameters sent to the Ajax rename request. Argument to this methods should be an associative array. Example:
tree.setAdditionalRenameRequestParameters({
name: "treeAction",
command: "renameNode"
});
which will add the variables name and command to the request sent to the server when an item is being renamed. - setAdditionalDeleteRequestParameters = Add parameters sent to the Ajax delete request. Argument to this methods should be an associative array. Example:
tree.setAdditionalRenameRequestParameters({
name: "treeAction",
command: "deleteNode"
});
which will add the variables name and command to the request sent to the server when an item is being deleted.
Save a tree
When you want to save the changes you made, you need to create your own save function. Here's an example how it is done in the demo(drag-drop-folder-tree.html):
//--------------------------------
// Save functions
//--------------------------------
var ajaxObjects = new Array();
function saveMyTree()
{
saveString = treeObj.getNodeOrders();
var ajaxIndex = ajaxObjects.length;
ajaxObjects[ajaxIndex] = new sack();
var url = 'saveNodes.php?saveString=' + saveString;
ajaxObjects[ajaxIndex].requestFile = url; // Specifying which file to get
ajaxObjects[ajaxIndex].onCompletion = function() { saveComplete(ajaxIndex); } ; // Specify function that will be executed after file has been found
ajaxObjects[ajaxIndex].runAJAX(); // Execute AJAX function
}
function saveComplete(index)
{
alert(ajaxObjects[index].response);
}
This will send the nodes to the server by use of Ajax.
You may also use a plain form when you submit the data. Then the saveMyTree function could look something like this:
function saveMyTree_byForm()
{
document.myForm.elements['saveString'].value = treeObj.getNodeOrders();
document.myForm.submit();
}
Update log
- Nov, 10th, 2006: Added delete and rename features
Comments
Post your comment
Please wait...



on line:364 in drag-dop-folder-tree.js
(after moveDragableNodes : function(e)
{ )
past this:
saveString = JSTreeObj.getNodeOrders();
var ajaxIndex = JSTreeObj.ajaxObjects.length;
JSTreeObj.ajaxObjects[ajaxIndex] = new sack();
var url = 'saveNodes.php?saveString=' + saveString;
JSTreeObj.ajaxObjects[ajaxIndex].requestFile = url; // Specifying which file to get
JSTreeObj.ajaxObjects[ajaxIndex].onCompletion = function() { JSTreeObj.saveComplete(ajaxIndex,obj); } ; // Specify function that will be executed after file has been found
JSTreeObj.ajaxObjects[ajaxIndex].runAJAX(); // Execute AJAX function
so from "<? ...file content... ?>"
to "<?php ...file content... ?>"
Only thing I'd add is a create new child item on the menu.
many thanks
I have one doubt.
when i try to get the node orders.
i get the id orders but the orders will have only numeric contents.
i trying to retrieve the data through asp.net
regards,
suraj
var url = 'http://adfs.pic.com.tw/mtree/saveNodes.asp?saveString=' + saveString;
and this is my asp file
<html>
<head>
<title>Test String</title>
</head>
<body>
<%
st = Request.QueryString("saveString")
Response.Write saveString
%>
</body>
</html>
But I only get this messages
http://adfs.pic.com.tw/dragdropmenu-01.jpg
How can I fix the problem ?
I find one error in my asp file
this new asp file.
<html>
<head>
<title>Test String</title>
</head>
<body>
<%
st = Request.QueryString("saveString")
Response.Write st
%>
</body>
</html>
But I only get a string like
http://adfs.pic.com.tw/dragdropmenu-02.jpg
How can I make the order effected with new one ?
10-0, 11-10, 12-10, 1-0, 2-0, 3-2
is it possible to have ther ordered number as well like this?
10-0-0, 11-10-0, 12-10-1, 1-0-1, 2-0-2, 3-2-0
I didn't find any code to generate the list's in this zip. (maybe I didn't search very good)
I wrote this to make an auto-list-tree based on the information in the database. It's not very "good" but it works. (I didn't add any of the possible options because I didn't need them)
function makeUnnumberedList($a){
$result = mysql_query("SELECT * FROM nodes WHERE parentID=$a");
if(mysql_num_rows($result) == 0)
{
// nothing
}
else
{
echo "<ul>";
}
while($row = mysql_fetch_array($result))
{
echo '<li id="node'.$row['ID'].'">'.'<a href=#>'.$row['title'].'</a>';
makeUnnumberedList($row['ID']);
echo '</li>';
}
if(mysql_num_rows($result) == 0)
{
// nothing
}
else
{
echo "</ul>";
}
}
I need it to accept nodes with alpha as well as numbers. It seems to strip out the alpha.
"node" as the prefix. Include your own code just after the node.
Then, in drag-drop-folder-tree.js, just after
,getNodeOrders : function(initObj,saveString) {
change
var numericID = li.id.replace(/[^0-9]/gi,'');
to
var numericID = li.id.replace(/node/,'');
And, right after that, change
var numericParentID = li.parentNode.parentNode.id.replace(/[^0-9]/gi,'');
to
var numericParentID = li.parentNode.parentNode.id.replace(/node/gi,'');
Very nice :)
p.s. I needed this because my database sorts the tree as follows:
1
.11
.12
..121
..122
2
.21
.22
3
.31
..311
..312
etc. When the sibling nodes go past 9, they then use the alpha,
'a', 'b', etc.
THANK YOU for such a great code!
When i put the <ul> tree in a div of master.page the script have same problem
------------
@Danix: Say what?!
------------
I got another question:
Is it possible when you reach the maximum depth, to place the items on the max level? (Now it gives a message 'maximum depth reached'..
Thankyou for the answer!
But, I have found one thing that does not work that well.
If you set a node to "no children", you cant drag other nodes as childs. OK
But if the "no children"-node already has childrens you can add the moving node as a sibling to the existing childrens.
I have tried to fix this in "moveDragableNodes" (see main js-file). But I have failed.
So anyone that have an idea?
Thanks /Johan
/Johan
I have a question, is possible expand the menu when click on name folder, not plus?
Thanks.
Andrew
Add
this.addEvent(aTag, 'click', this.showHideNode);
on line 796, just below
aTag.id = 'nodeATag' + menuItems[no].id.replace(/[^0-9]/gi,'');
I think that should do the trick. I have updated the demo and the zip file with this fix.
the script is great, but can you please let me know if is there also a possibility to place on the same page two or more independent trees with the drag-drop funcionality?
Each of the trees on the page would have its own parents and chindren and the dragged children of the focused tree will be possible to save in the DB.
If yes, plese give me a hint how to do it.
Thanks in advance for your support
Rado
Is it possible to add the ADD function same as DELETE and REMOVE. Would really apprecaite it.
Thanks
And how can I add new folder? :-)
onError returns my message to me too. the php file and the html file are in the root directory and the js file is in the subdirectory of js.
still m not able to delete the node.
can anyone help me?..........
I hope someone can give me a quick answer for this :-).
I have two trees and i can drag across fine and everything works perfect. My only snag is I would like my first tree to remain a static list so i can pull over the same node more than once but as soon as I drag the node over its removed from the first tree.
Any ideas how I can stop this?
Mark
but when i tried to add Root node in side the <div> tag then i am getting the Root node in nect line.
i tried adding table but got the same result its like
i doing changes in .html file.
<li id="node0" noDrag="true" noSiblings="true" noDelete="true" noRename="true"><a href="#"><div><table><tr><td>Root node</td></tr></table></div></a>
i am getting the Root ode in next line.
Please suggest/add some snippet to add <div><table> tag
But the demo I downloaded is <strong>not deleting the nodes even though it shows the confirmation box too</strong>.Could any one plzz help me to figure out the problem.