Newer
Older
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
<title>Fancytree - Example</title>
<script src="../lib/jquery.js"></script>
<script src="../lib/jquery-ui.custom.js"></script>
<link href="../src/skin-win8/ui.fancytree.css" rel="stylesheet">
<script src="../src/jquery.fancytree.js"></script>
<!-- Start_Exclude: This block is not part of the sample code -->
<link href="../lib/prettify.css" rel="stylesheet">
<script src="../lib/prettify.js"></script>
<link href="sample.css" rel="stylesheet">
<script src="sample.js"></script>
<!-- End_Exclude -->
<script type="text/javascript">
$(function(){
// Initialize the tree inside the <div>element.
// The tree structure is read from the contained <ul> tag.
$("#tree").fancytree({
checkbox: true,
activate: function(event, data) {
$("#echoActive").text(data.node.title);
// alert(node.getKeyPath());
if( data.node.url )
window.open(data.node.url, data.node.target);
},
deactivate: function(event, data) {
$("#echoSelected").text("-");
},
focus: function(event, data) {
$("#echoFocused").text(data.node.title);
},
blur: function(event, data) {
$("#echoFocused").text("-");
},
lazyLoad: function(event, data){
// Simulate a slow ajax request
var dfd = new $.Deferred();
data.result = dfd.promise();
window.setTimeout(function(){
dfd.resolve([
{ title: "Lazy node 1", lazy: true },
{ title: "Simple node 2", select: true }
]);
}, 1500);
}
});
});
</script>
<!-- Start_Exclude: This block is not part of the sample code -->
<script>
$(function(){
addSampleButton({
label: "Disable",
id: "btnDisable",
code: function(){
if( $("#tree").fancytree("option", "disabled") ){
$("#tree").fancytree("enable");
$("#btnDisable").text("Disable");
}else{
$("#tree").fancytree("disable");
$("#btnDisable").text("Enable");
}
}
});
addSampleButton({
label: "Expand all",
newline: false,
code: function(){
$("#tree").visit(function(node){
node.setExpanded();
});
}
});
addSampleButton({
label: "Collapse all",
newline: false,
code: function(){
$("#tree").visit(function(node){
node.setExpanded(false);
});
}
});
addSampleButton({
label: "Toggle expand",
code: function(){
$("#tree").visit(function(node){
node.toggleExpanded();
});
}
});
addSampleButton({
label: "tree.getActiveNode()",
newline: false,
code: function(){
var node = $("#tree").fancytree("getActiveNode");
if( node ){
alert("Currently active: " + node.title);
}else{
alert("No active node.");
}
}
});
addSampleButton({
label: "tree.toDict()",
code: function(){
// Convert the whole tree into an dictionary
var tree = $("#tree").fancytree("getTree");
var d = tree.toDict(true);
alert(JSON.stringify(d));
}
});
addSampleButton({
label: "activateKey('id4.3.2')",
code: function(){
$("#tree").fancytree("getTree").activateKey("id4.3.2");
// also possible:
// $("#tree").fancytree("getTree").getNodeByKey("id4.3.2").setActive();
}
});
addSampleButton({
label: "setTitle()",
code: function(){
var node = $("#tree").fancytree("getActiveNode");
if( !node ) return;
node.setTitle(node.title + ", " + new Date());
// this is a shortcut for
// node.fromDict({title: data.node.title + new Date()});
}
});
addSampleButton({
label: "Sort tree",
newline: false,
code: function(){
var node = $("#tree").fancytree("getRootNode");
node.sortChildren(null, true);
}
});
addSampleButton({
label: "Sort active banch",
code: function(){
var node = $("#tree").fancytree("getActiveNode");
// Custom compare function (optional) that sorts case insensitive
var cmp = function(a, b) {
a = a.title.toLowerCase();
b = b.title.toLowerCase();
return a > b ? 1 : a < b ? -1 : 0;
};
node.sortChildren(cmp, false);
}
});
addSampleButton({
header: "Create nodes",
tooltip: "Use node.addChildren() with single objects",
label: "Add single nodes",
newline: false,
code: function(){
// Sample: add an hierarchic branch using code.
// This is how we would add tree nodes programatically
var rootNode = $("#tree").fancytree("getRootNode");
var childNode = rootNode.addChildren({
title: "Programatically addded nodes",
tooltip: "This folder and all child nodes were added programmatically.",
folder: true
});
childNode.addChildren({
title: "Document using a custom icon",
icon: "customdoc1.gif"
});
}
});
addSampleButton({
tooltip: "Use node.appendSibling()",
label: "Apppend a sibling node",
newline: false,
code: function(){
var tree = $("#tree").fancytree("getTree"),
node = tree.getActiveNode(),
newData = {title: "New Node"},
newSibling = node.appendSibling(newData);
}
});
addSampleButton({
label: "ROOT.addChildren()",
tooltip: "Use node.addChildren() with recursive arrays",
code: function(){
// Sample: add an hierarchic branch using an array
var obj = [
{ title: "Lazy node 1", lazy: true },
{ title: "Lazy node 2", lazy: true },
{ title: "Folder node 3", folder: true,
children: [
{ title: "node 3.1" },
{ title: "node 3.2",
children: [
{ title: "node 3.2.1" },
{ title: "node 3.2.2",
children: [
{ title: "node 3.2.2.1" }
]
}
]
}
]
}
];
$("#tree").fancytree("getRootNode").addChildren(obj);
}
});
addSampleButton({
label: "node.fromDict()",
code: function(){
var node = $("#tree").fancytree("getActiveNode");
if( !node ) return;
// Set node data and - optionally - replace children
node.fromDict({
title: node.title + new Date(),
children: [{title: "t1"}, {title: "t2"}]
});
}
});
CLIPBOARD = null;
addSampleButton({
label: "Clipboard = node.toDict()",
newline: false,
code: function(){
// Convert active node (and descendants) to a dictionary and store
// in
var node = $("#tree").fancytree("getActiveNode");
var d = node.toDict(true, function(dict){
// Remove keys, so they will be re-generated when this dict is
// passed to addChildren()
delete dict.key;
});
// Store in a globael variable
CLIPBOARD = d;
alert("CLIPBOARD = " + JSON.stringify(d));
}
});
addSampleButton({
label: "node.fromDict(Clipboard)",
code: function(){
var node = $("#tree").fancytree("getActiveNode");
if( !node ) return;
// Set node data and - optionally - replace children
node.fromDict(CLIPBOARD);
}
});
addSampleButton({
label: "Remove selected nodes (but keep children)",
newline: true,
code: function(){
var tree = $("#tree").fancytree("getTree"),
selNodes = tree.getSelectedNodes();
selNodes.forEach(function(node) {
while( node.hasChildren() ) {
node.getFirstChild().moveTo(node.parent, "child");
}
node.remove();
});
}
});
});
</script>
<!-- End_Exclude -->
</head>
<body class="example">
<h1>Fancytree API</h1>
<div class="description">
Demonstrate some Fancytree and FancytreeNode API methods.
<br>
See the <a href="https://github.com/mar10/fancytree/wiki/TutorialApi"
target="_blank" class="external">API Tutorial</a>
for details.
</div>
<div>
<label for="skinswitcher">Skin:</label> <select id="skinswitcher"></select>
</div>
<div id="tree">
<ul>
<li>This simple node (and the following) have been created from html.
<li id="id1" title="This is item #1">item1 with key and tooltip
<li id="id2">item2 with key "id2"
<li id="id3" class="folder">Standard Folder with some children
<ul>
<li id="id3.1">Sub-item 3.1
<li id="id3.2">Sub-item 3.2
</ul>
<li id="id4">item 4. Note that also non-folders (i.e. 'documents') may have child nodes
<ul>
<li id="id4.1">Sub-item 4.1
<li id="id4.2">Sub-item 4.2
<li id="id4.3">Sub-item 4.3
<ul>
<li id="id4.3.1">Sub-item 4.3.1
<li id="id4.3.2">Sub-item 4.3.2
<ul>
<li id="id4.3.2.1">Sub-item 4.3.2.1
<li id="id4.3.2.2">Sub-item 4.3.2.2
</ul>
</ul>
<li id="id4.4">Sub-item 4.4
</ul>
<li id="id5" class="expanded folder">Advanced examples
<ul>
<li data="key: 'node5.1'">item5.1: Using data attribute as an alternative way to specify a key.
<li data="key: 'node5.3', folder: true">item5.1: Using data attribute as an alternative way to specify a folder.
<li id="id5.2">Sub-item 5.2
<li>Item without a key. Keys are optional (generated automatically), but may be used in the callbacks
</ul>
</ul>
</div>
<div>Active node: <span id="echoActive">-</span></div>
<div>Focused node: <span id="echoFocused">-</span></div>
<!-- Start_Exclude: This block is not part of the sample code -->
<p id="sampleButtons">
</p>
<hr>
<p class="sample-links no_code">
<a class="hideInsideFS" href="https://github.com/mar10/fancytree">jquery.fancytree.js project home</a>
<a class="hideOutsideFS" href="#">Link to this page</a>
<a class="hideInsideFS" href="index.html">Example Browser</a>
<a href="#" id="codeExample">View source code</a>
</p>
<pre id="sourceCode" class="prettyprint" style="display:none"></pre>
<!-- End_Exclude -->
</body>
</html>