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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
<title>Multiple Extensions - Fancytree</title>
<link href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css" rel="stylesheet">
<script src="//code.jquery.com/jquery-3.2.1.min.js"></script>
<script src="//code.jquery.com/ui/1.12.1/jquery-ui.min.js"></script>
<!-- jquery-contextmenu (https://github.com/mar10/jquery-ui-contextmenu/) -->
<script src="//cdn.jsdelivr.net/jquery.ui-contextmenu/1/jquery.ui-contextmenu.min.js"></script>
<link href="../src/skin-win8/ui.fancytree.css" rel="stylesheet">
<script src="../src/jquery.fancytree.js"></script>
<script src="../src/jquery.fancytree.dnd.js"></script>
<script src="../src/jquery.fancytree.edit.js"></script>
<script src="../src/jquery.fancytree.gridnav.js"></script>
<script src="../src/jquery.fancytree.table.js"></script>
<!--
<script src="../../build/jquery.fancytree-all.min.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="../demo/sample.css" rel="stylesheet">
<script src="../demo/sample.js"></script>
<!-- End_Exclude -->
<style type="text/css">
.ui-menu {
width: 180px;
font-size: 63%;
}
.ui-menu kbd { /* Keyboard shortcuts for ui-contextmenu titles */
float: right;
}
/* custom alignment (set by 'renderColumns'' event) */
td.alignRight {
text-align: right;
}
td.alignCenter {
text-align: center;
}
td input[type=input] {
width: 40px;
}
</style>
<script type="text/javascript">
var CLIPBOARD = null;
/*
SOURCE = [
{title: "node 1", folder: true, expanded: true, children: [
{title: "node 1.1", foo: "a"},
{title: "node 1.2", foo: "b"}
]},
{title: "node 2", folder: true, expanded: false, children: [
{title: "node 2.1", foo: "c"},
{title: "node 2.2", foo: "d"}
]}
];
*/
$(function(){
$("#tree").fancytree({
checkbox: true,
titlesTabbable: true, // Add all node titles to TAB chain
quicksearch: true, // Jump to nodes when pressing first character
// source: SOURCE,
source: { url: "ajax-tree-products.json"},
extensions: ["edit", "dnd", "table", "gridnav"],
dnd: {
preventVoidMoves: true,
preventRecursiveMoves: true,
autoExpandMS: 400,
dragStart: function(node, data) {
return true;
},
dragEnter: function(node, data) {
// return ["before", "after"];
return true;
},
dragDrop: function(node, data) {
data.otherNode.moveTo(node, data.hitMode);
}
},
edit: {
triggerStart: ["f2", "shift+click", "mac+enter"],
close: function(event, data) {
if( data.save && data.isNew ){
// Quick-enter: add new nodes until we hit [enter] on an empty title
$("#tree").trigger("nodeCommand", {cmd: "addSibling"});
}
}
},
table: {
indentation: 20,
nodeColumnIdx: 2,
checkboxColumnIdx: 0
},
gridnav: {
autofocusInput: false,
handleCursorKeys: true
},
lazyLoad: function(event, data) {
data.result = {url: "../demo/ajax-sub2.json"};
},
createNode: function(event, data) {
var node = data.node,
$tdList = $(node.tr).find(">td");
// Span the remaining columns if it's a folder.
// We can do this in createNode instead of renderColumns, because
// the `isFolder` status is unlikely to change later
if( node.isFolder() ) {
$tdList.eq(2)
.prop("colspan", 6)
.nextAll().remove();
}
},
renderColumns: function(event, data) {
var node = data.node,
$tdList = $(node.tr).find(">td");
// (Index #0 is rendered by fancytree by adding the checkbox)
// Set column #1 info from node data:
$tdList.eq(1).text(node.getIndexHier());
// (Index #2 is rendered by fancytree)
// Set column #3 info from node data:
$tdList.eq(3).find("input").val(node.key);
$tdList.eq(4).find("input").val(node.data.foo);
// Static markup (more efficiently defined as html row template):
// $tdList.eq(3).html("<input type='input' value='" + "" + "'>");
// ...
}
}).on("nodeCommand", function(event, data){
// Custom event handler that is triggered by keydown-handler and
// context menu:
var refNode, moveMode,
tree = $(this).fancytree("getTree"),
node = tree.getActiveNode();
switch( data.cmd ) {
case "moveUp":
refNode = node.getPrevSibling();
if( refNode ) {
node.moveTo(refNode, "before");
node.setActive();
}
break;
case "moveDown":
refNode = node.getNextSibling();
if( refNode ) {
node.moveTo(refNode, "after");
node.setActive();
}
break;
case "indent":
refNode = node.getPrevSibling();
if( refNode ) {
node.moveTo(refNode, "child");
refNode.setExpanded();
node.setActive();
}
break;
case "outdent":
if( !node.isTopLevel() ) {
node.moveTo(node.getParent(), "after");
node.setActive();
}
break;
case "rename":
node.editStart();
break;
case "remove":
refNode = node.getNextSibling() || node.getPrevSibling() || node.getParent();
node.remove();
if( refNode ) {
refNode.setActive();
}
break;
case "addChild":
node.editCreateNode("child", "");
break;
case "addSibling":
node.editCreateNode("after", "");
break;
case "cut":
CLIPBOARD = {mode: data.cmd, data: node};
break;
case "copy":
CLIPBOARD = {
mode: data.cmd,
data: node.toDict(function(n){
delete n.key;
})
};
break;
case "clear":
CLIPBOARD = null;
break;
case "paste":
if( CLIPBOARD.mode === "cut" ) {
// refNode = node.getPrevSibling();
CLIPBOARD.data.moveTo(node, "child");
CLIPBOARD.data.setActive();
} else if( CLIPBOARD.mode === "copy" ) {
node.addChildren(CLIPBOARD.data).setActive();
}
break;
default:
alert("Unhandled command: " + data.cmd);
return;
}
// }).on("click dblclick", function(e){
// console.log( e, $.ui.fancytree.eventToString(e) );
}).on("keydown", function(e){
var cmd = null;
// console.log(e.type, $.ui.fancytree.eventToString(e));
switch( $.ui.fancytree.eventToString(e) ) {
case "ctrl+shift+n":
case "meta+shift+n": // mac: cmd+shift+n
cmd = "addChild";
break;
case "ctrl+c":
case "meta+c": // mac
cmd = "copy";
break;
case "ctrl+v":
case "meta+v": // mac
cmd = "paste";
break;
case "ctrl+x":
case "meta+x": // mac
cmd = "cut";
break;
case "ctrl+n":
case "meta+n": // mac
cmd = "addSibling";
break;
case "del":
case "meta+backspace": // mac
cmd = "remove";
break;
// case "f2": // already triggered by ext-edit pluging
// cmd = "rename";
// break;
case "ctrl+up":
cmd = "moveUp";
break;
case "ctrl+down":
cmd = "moveDown";
break;
case "ctrl+right":
case "ctrl+shift+right": // mac
cmd = "indent";
break;
case "ctrl+left":
case "ctrl+shift+left": // mac
cmd = "outdent";
}
if( cmd ){
$(this).trigger("nodeCommand", {cmd: cmd});
// e.preventDefault();
// e.stopPropagation();
return false;
}
});
/*
* Tooltips
*/
// $("#tree").tooltip({
// content: function () {
// return $(this).attr("title");
// }
// });
/*
* Context menu (https://github.com/mar10/jquery-ui-contextmenu)
*/
$("#tree").contextmenu({
delegate: "span.fancytree-node",
menu: [
{title: "Edit <kbd>[F2]</kbd>", cmd: "rename", uiIcon: "ui-icon-pencil" },
{title: "Delete <kbd>[Del]</kbd>", cmd: "remove", uiIcon: "ui-icon-trash" },
{title: "----"},
{title: "New sibling <kbd>[Ctrl+N]</kbd>", cmd: "addSibling", uiIcon: "ui-icon-plus" },
{title: "New child <kbd>[Ctrl+Shift+N]</kbd>", cmd: "addChild", uiIcon: "ui-icon-arrowreturn-1-e" },
{title: "----"},
{title: "Cut <kbd>Ctrl+X</kbd>", cmd: "cut", uiIcon: "ui-icon-scissors"},
{title: "Copy <kbd>Ctrl-C</kbd>", cmd: "copy", uiIcon: "ui-icon-copy"},
{title: "Paste as child<kbd>Ctrl+V</kbd>", cmd: "paste", uiIcon: "ui-icon-clipboard", disabled: true }
],
beforeOpen: function(event, ui) {
var node = $.ui.fancytree.getNode(ui.target);
$("#tree").contextmenu("enableEntry", "paste", !!CLIPBOARD);
node.setActive();
},
select: function(event, ui) {
var that = this;
// delay the event, so the menu can close and the click event does
// not interfere with the edit control
setTimeout(function(){
$(that).trigger("nodeCommand", {cmd: ui.cmd});
}, 100);
}
});
});
</script>
</head>
<body class="example">
<h1>Example: tree grid with keyboard navigation, DnD, and editing capabilites </h1>
<div class="description">
Bringing it all together: this sample combines different extensions and
custom events to implement an editable tree grid:
<ul>
<li>'ext-dnd' to re-order nodes using drag-and-drop.</li>
<li>'ext-table' + 'ext-gridnav' to implement a tree grid.<br>
Try <kbd>UP / DOWN / LEFT / RIGHT</kbd>, <kbd>TAB</kbd>, <kbd>Shift+TAB</kbd>
to navigate between grid cells. Note that embedded input controls
remain functional.
</li>
<li>'ext-edit': inline editing.<br>
Try <kbd>F2</kbd> to rename a node.<br>
<kbd>Ctrl+N</kbd>, <kbd>Ctrl+Shift+N</kbd> to add nodes (Quick-enter: add new nodes until
[enter] is hit on an empty title).
</li>
<li>Extended keyboard shortcuts:<br>
<kbd>Ctrl+C</kbd>, <kbd>Ctrl+X</kbd>, <kbd>Ctrl+P</kbd> for copy/paste,<br>
<kbd>Ctrl+UP</kbd>, <kbd>Ctrl+DOWN</kbd>, <kbd>Ctrl+LEFT</kbd>, <kbd>Ctrl+RIGHT</kbd> to move nodes around and change indentation.
</li>
<li>3rd-party <a href="https://github.com/mar10/jquery-ui-contextmenu">contextmenu</a> for additional edit commands</li>
</ul>
</div>
<div>
<label for="skinswitcher">Skin:</label> <select id="skinswitcher"></select>
</div>
<h1>Table Tree</h1>
<div>
<label>Fake input:<input id="input1"/></label>
</div>
<table id="tree">
<colgroup>
<col width="30px">
<col width="50px">
<col width="350px">
<col width="50px">
<col width="50px">
<col width="30px">
<col width="30px">
<col width="50px">
</colgroup>
<thead>
<tr> <th></th> <th>#</th> <th></th> <th>Ed1</th> <th>Ed2</th> <th>Rb1</th> <th>Rb2</th> <th>Cb</th></tr>
</thead>
<tbody>
<!-- Define a row template for all invariant markup: -->
<tr>
<td class="alignCenter"></td>
<td></td>
<td></td>
<td><input name="input1" type="input"></td>
<td><input name="input2" type="input"></td>
<td class="alignCenter"><input name="cb1" type="checkbox"></td>
<td class="alignCenter"><input name="cb2" type="checkbox"></td>
<td>
<select name="sel1" id="">
<option value="a">A</option>
<option value="b">B</option>
</select>
</td>
</tr>
</tbody>
</table>
<!-- Start_Exclude: This block is not part of the sample code -->
<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>