Scriptaculous SortableTree
posted: May 30th, 2008 · by: Sven
For a project I’ve been working on recently I needed a sortable javascript tree widget that allows to drag and drop tree nodes and serialize an ajax call on drop events.
I’ve googled for solutions but did not find anything that implemented the simple things I wanted in a lightweight manner. So I started to check out the Scriptaculous sortable tree option but no matter what I tried I could not get it behave.
A couple of frustrating hours of reading the code and comments on the somewhat sparse Scriptaculous documentation (no offense!) I gave up and decided to implement this myself.
Here’s the result.
I’ve published this on GitHub: scriptaculous-sortabletree and there are two working demos online:
- Simple and bold
- Textmate drawer (same as above)
See demo/bold.html and demo/textmate.html for usage. If you’re familiar with Scriptaculous Draggables things should be pretty obvious :)
I haven’t included any fancy stuff (like configuring tree serialization to different query formats) because in Javascript one can always overwrite the default functions that are provided by the library with custom behaviour.
If you think something’s seriously missing though, please drop me a note!
Kristoff said June 11th, 2008 at 05:56 PM ¶
Sir, you just saved me countless hours of pointless tinkering. Thank you very much for sharing this resource and if you ever happen to come through this part of the old continent, I insist on buying you a drink. Thanks again!
Luke said June 19th, 2008 at 03:34 PM ¶
Cool, this is exactly what i needed. Thanks.
Sven said June 20th, 2008 at 11:18 AM ¶
Nice to see this stuff is useful to you guys. Please let me know if there’s something to improve!
Kristoff, thanks. I’ve actually never been to Belgium. But if I ever do I’ll get back to you. ;)
Ossi Talonpoika said August 12th, 2008 at 03:32 PM ¶
I have spent whole day trying to find this. I was already considering to write whole thing by myself. Thank you!
V said August 18th, 2008 at 01:23 PM ¶
Is this working on IE too? From what I’ve seen is throwing an error..
instant.bonsai said August 18th, 2008 at 01:40 PM ¶
I had the same problem a few days ago. You have to remove the komma behind the bracket in line 63 to fix this.
V said August 18th, 2008 at 02:05 PM ¶
I’m receiving another error now, telling me that “Object doesn’t support this action”.
Pol said October 7th, 2008 at 10:38 AM ¶
Hello,
How to serialize the tree ?
I would like to have the position of each tree id…
Is it possible ?
Thanks !
Zaid said November 15th, 2008 at 11:28 PM ¶
Hey - im getting the “Object doesn’t support this action” in IE too (with the rogue comma removed) - has anyone made progress with this at all?
nullify said December 9th, 2008 at 03:27 PM ¶
Wonderful stuff! I will definitely implement this in my own page cms which also uses your superb routing-filter plugin. Keep up the good work!
Ryan said December 31st, 2008 at 09:00 PM ¶
for everyone getting the “Object doesn’t support this action” error in IE: this actually seems like a problem with the dragdrop.js file that is bundled when you download this. It looks like it is an older version of the script.aculo.us library. All you need to do to fix the problem is go to github and get the most current version of scriptaculous and replace dragdrop.js with the dragdrop.js from the newer version of scriptaculous.
thb said February 10th, 2009 at 12:49 PM ¶
awesome, man!
Thanks a lot!
Chris said March 11th, 2009 at 12:25 AM ¶
Should add
this.tree.unmark_all();
to the onDrop call so that the script cleans up after itself once the drop is done.
I was adding padding as the drag was happening depending on hover top bottom or insert and was having the css markers stay causing padding to remain after drop was finished.
css markers would not clear until another drag was begun.
Rasmus said March 28th, 2009 at 09:56 AM ¶
Hi, great stuff. It’ll probably be used in an upcoming version of bromine (bromine.openqa.org)if i can figure out how to put a css class limit on the nodes that are draggable. Any clues?
var tree = new SortableTree(‘tree’, { draggable: ‘:not(.tc)’ , droppable: { container: ‘:not(.tc)’ },
the droppable stuff works, but not the draggable. Cheers, Rasmus
Aldrin Cabrera said May 8th, 2009 at 03:37 AM ¶
Thanks dude! Thank you so much!
Marc said June 1st, 2009 at 11:20 PM ¶
Awesome script, saved me a lot of hours of banging my head against my desk, top notch work
Michal Palma said June 4th, 2009 at 01:45 AM ¶
Man this is funny. scriptaculous wrote someone named Tomas Fuchs
Ryan said June 19th, 2009 at 04:52 PM ¶
I’ve been using this script for a while – it works great – but there’s one little thing I can’t seem to resolve. The entire containing element (in the example above - the box around the tree) is draggable, rather than just its contents. This gives some weird results when you try to drop it inside of itself.
Guillaume said July 24th, 2009 at 04:27 PM ¶
Thanks, This works great. However, I’m trying to add list items thru a button let’s say, (appendChild, etc.), but the newly created item seems not to be draggable at all. Is there any function to call back once the list has been appended ? Or better, is there any function in the class that would let me create new list elements ? I’m pretty new to js and i can look at the code for quite long without finding any solution…
Thanks
roney said August 8th, 2009 at 08:48 AM ¶
Hi,
I just want to know , How can i stop this sortable package to create sub points upto two levels.
Thanks Roney
Kieran Simkin said November 21st, 2009 at 01:52 AM ¶
Guillaume, I’ve being dynamically adding elements using the following code, assuming $(‘categorytree’) references the
of your tree and category
manager_tree is the SortableTree object itself.However, I am now having some troubles with my onDrag handler and I wish to ask if anything about SortableTree makes any assumptions about the ID values of each
Kieran Simkin said November 21st, 2009 at 01:56 AM ¶
Guillaume, I’ve being dynamically adding elements using the following code, assuming $(‘categorytree’) references the <ul> of your tree and categorymanager_tree is the SortableTree object itself.
However, I am now having some troubles with my onDrag handler and I wish to ask if anything about SortableTree makes any assumptions about the ID values of each <li> element eg (node1, node11, node_2 etc)? I notice they all follow a pattern in the examples, but I’m just using database IDs for the numerical ID of each of my <li> elements, could this be causing weird behaviour?