TafelTree-view (Javascript)
Treeview based on the library Script Aculous
Samples
Basic Drag & Drop
Let's see how to do a simple drag&drop for one tree
1 |
---|
2 |
---|
3 |
---|
4 |
---|
5 |
---|
6 |
---|
7 |
---|
8 |
---|
9 |
---|
10 |
---|
11 |
---|
12 |
---|
13 |
---|
14 |
---|
15 |
---|
16 |
---|
17 |
---|
18 |
---|
19 |
---|
20 |
---|
21 |
---|
22 |
---|
23 |
---|
24 |
---|
25 |
---|
26 |
---|
27 |
---|
28 |
---|
29 |
---|
30 |
---|
31 |
---|
32 |
---|
33 |
---|
34 |
---|
35 |
---|
36 |
---|
37 |
---|
38 |
---|
39 |
---|
| var tree1 = null; | | | | | | | | | function funcDrop1 (move, drop, finished) { | if (!finished) { | if (confirm('Confirm drop ?')) { | return true; | } else { | return false; | } | } else { | alert ('Drop is done'); | } | } | | | | | function TafelTreeInit () { | tree1 = new TafelTree ('drag1', struct1, { | 'generate' : true, | 'imgBase' : 'imgs/', | 'defaultImg' : 'page.gif', | 'defaultImgOpen' : 'folderopen.gif', | 'defaultImgClose' : 'folder.gif', | 'onDrop' : funcDrop1, | 'behaviourDrop' : 'child', | | 'dropALT' : true, | 'dropCTRL' : true | }); | } | |
|
Basic Drag & Drop between two trees
It's possible to do some drag&drop between two or more trees. You can see below how to do for a drop between two trees.
1 |
---|
2 |
---|
3 |
---|
4 |
---|
5 |
---|
6 |
---|
7 |
---|
8 |
---|
9 |
---|
10 |
---|
11 |
---|
12 |
---|
13 |
---|
14 |
---|
15 |
---|
16 |
---|
17 |
---|
18 |
---|
19 |
---|
20 |
---|
21 |
---|
22 |
---|
23 |
---|
24 |
---|
25 |
---|
26 |
---|
27 |
---|
28 |
---|
29 |
---|
30 |
---|
31 |
---|
32 |
---|
33 |
---|
34 |
---|
35 |
---|
36 |
---|
37 |
---|
38 |
---|
39 |
---|
40 |
---|
41 |
---|
42 |
---|
43 |
---|
| | | | function funcDrop2 (move, drop, finished) { | | if (finished) { | var _drag = move.getText(); | var _drop = drop.getText(); | var _treeMove = move.tree.id; | var _treeDrop = drop.tree.id; | alert ('Dropped ' + _drag + ' (' + _treeMove + ') on ' | + _drop + ' ('+ _treeDrop + ')'); | } | | | return true; | } | | | | | | function TafelTreeInit () { | tree1b = new TafelTree ('drag1b', struct1, { | 'generate' : true, | 'imgBase' : 'imgs/', | 'defaultImg' : 'page.gif', | 'defaultImgOpen' : 'folderopen.gif', | 'defaultImgClose' : 'folder.gif', | 'onDrop' : funcDrop2 | }); | tree2 = new TafelTree ('drag2', struct2, { | 'generate' : true, | 'rtlMode' : true, | 'imgBase' : 'imgs/', | 'defaultImg' : 'globe.gif', | 'defaultImgOpen' : 'imgfolder.gif', | 'defaultImgClose' : 'folder.gif', | 'onDrop' : funcDrop2, | 'bind' : [tree1] | }); | } | |
|
Copy Drag & Drop
We cna make copy-drag&drop where the branch is copied instead of moved. By default, you have to press CTRL while dropping the branch. You can set the copyDrag behaviour as a default behaviour by using setBehaviourDrop() function. In this sample, we will manipulate a copied branch in a drag&drop environment.
1 |
---|
2 |
---|
3 |
---|
4 |
---|
5 |
---|
6 |
---|
7 |
---|
8 |
---|
9 |
---|
10 |
---|
11 |
---|
12 |
---|
13 |
---|
14 |
---|
15 |
---|
16 |
---|
17 |
---|
18 |
---|
19 |
---|
20 |
---|
21 |
---|
22 |
---|
23 |
---|
24 |
---|
25 |
---|
26 |
---|
27 |
---|
28 |
---|
29 |
---|
30 |
---|
31 |
---|
| | | | function funcDropCopy (move, drop, finished, copy) { | | | if (finished) { | if (copy) { | | copy.setText(move.getText() + " copied"); | } | } | return true; | } | | | | | | function TafelTreeInit () { | treeCopy = new TafelTree ("dragCopy", structCopy, { | "generate" : true, | "imgBase" : "imgs/", | "defaultImg" : "page.gif", | "defaultImgOpen" : "folderopen.gif", | "defaultImgClose" : "folder.gif", | "onDrop" : funcDropCopy, | "behaviourDrop" : "childcopy" | }); | } | |
|
Ajax Drag & Drop 1
Drag&drop Ajax is a build-in function that hold server responses. Very useful when you need heavy controls before validating a drop.
Below the PHP file
1 |
---|
2 |
---|
3 |
---|
4 |
---|
5 |
---|
6 |
---|
7 |
---|
8 |
---|
9 |
---|
10 |
---|
11 |
---|
12 |
---|
13 |
---|
14 |
---|
15 |
---|
16 |
---|
17 |
---|
18 |
---|
19 |
---|
20 |
---|
21 |
---|
22 |
---|
23 |
---|
24 |
---|
25 |
---|
26 |
---|
27 |
---|
28 |
---|
29 |
---|
30 |
---|
31 |
---|
32 |
---|
33 |
---|
34 |
---|
35 |
---|
36 |
---|
37 |
---|
38 |
---|
39 |
---|
40 |
---|
41 |
---|
42 |
---|
43 |
---|
44 |
---|
45 |
---|
46 |
---|
47 |
---|
48 |
---|
49 |
---|
50 |
---|
51 |
---|
52 |
---|
53 |
---|
54 |
---|
| <?php | | $okDrop = false; | $msg = 'POST error'; | | | | | | | | | | | if (isset($_POST['drop'])) { | | | | | $dragID = $_POST['drag_id']; | $dropID = $_POST['drop_id']; | | | | if (!$_POST['sibling']){ | | | $okDrop = true; | $msg = 'All is ok'; | } else { | | | $okDrop = false; | $msg = 'Can t drop as sibling!'; | } | } | | | | | if ($okDrop) { | echo "({"; | echo "'ok' : true,"; | echo "'msg' : '".$msg."'"; | echo "})"; | } else { | echo "({"; | echo "'ok' : false,"; | echo "'msg' : '".$msg."'"; | echo "})"; | } | | ?> | |
|
And the Javascript code
1 |
---|
2 |
---|
3 |
---|
4 |
---|
5 |
---|
6 |
---|
7 |
---|
8 |
---|
9 |
---|
10 |
---|
11 |
---|
12 |
---|
13 |
---|
14 |
---|
15 |
---|
16 |
---|
17 |
---|
18 |
---|
19 |
---|
20 |
---|
21 |
---|
22 |
---|
23 |
---|
24 |
---|
25 |
---|
26 |
---|
27 |
---|
28 |
---|
29 |
---|
30 |
---|
| | function funcDrop3 (move, drop, response, finished) { | | | if (!finished) { | | | var obj = eval(response); | if (!obj.ok) { | alert ('Problem : ' + obj.msg); | return false; | } | } | return true; | } | | | | | function TafelTreeInit () { | tree3 = new TafelTree ('drag3', struct3, { | 'generate' : true, | 'imgBase' : 'imgs/', | 'defaultImg' : 'page.gif', | 'defaultImgOpen' : 'folderopen.gif', | 'defaultImgClose' : 'folder.gif', | 'onDropAjax' : [funcDrop3, 'server/sampleDrop1.php'] | }); | } | |
|
Try to make a drag&drop by maintaining ALT key pressed. You should see an error pop up.
Just notice that in this sample, a "dropALT : false" in the tree constructor would have the same behaviour. It was just for an easy understandable sample.
Documentation
A complete documentation is available here
Smarty
A Smarty module is developed for the tree. Thanks to Abalam! Module smarty
They're using it
Already some projects use this library.
Treeview JS TafelTree
2007-07-23, Tafel. Optimised for Mozilla Firefox