-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathjquery.treeTable.js
More file actions
90 lines (79 loc) · 2.14 KB
/
jquery.treeTable.js
File metadata and controls
90 lines (79 loc) · 2.14 KB
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
/*
* jQuery treeTable Plugin
*
* Copyright 2012, Grégoire Dubourg
* Dual licensed under the MIT or GPL Version 2 licenses.
*/
(function($) {
// Helps to make options available to all functions
var options;
$.fn.treeTable = function(opts) {
options = $.extend({}, $.fn.treeTable.defaults, opts);
return this.each(function() {
$(this).find("tbody tr").each(function() {
initialize($(this));
});
});
};
$.fn.treeTable.defaults = {
dataAttribute: "level",
collapsedByDefault: true,
ignoreClickOn: "input, a"
};
// Recursively hide all node's children in a tree
$.fn.collapse = function() {
if ($(this).hasChildren()) {
$(this).removeClass("expanded").addClass("collapsed");
childrenOf($(this)).each(function() {
$(this).hide().collapse();
});
}
return this;
};
// Recursively show all node's children in a tree
$.fn.expand = function() {
if ($(this).hasChildren()) {
$(this).removeClass("collapsed").addClass("expanded");
childrenOf($(this)).each(function() {
$(this).show();
});
}
return this;
};
// Check if node has children
$.fn.hasChildren = function() {
return (childrenOf($(this)).length > 0);
};
// Toggle an entire branch
$.fn.toggle = function() {
if ($(this).hasClass("collapsed"))
$(this).expand();
else
$(this).collapse();
return this;
};
// === Private functions
function initialize(node) {
if (node.hasChildren()) {
node.click(function(event) {
var $target = $(event.target);
if (!$target.is(options.ignoreClickOn)) {
node.toggle();
return false;
}
});
if (options.collapsedByDefault)
node.collapse();
else
node.expand();
}
};
function getLevel(node) {
return parseInt($(node).data(options.dataAttribute));
};
function childrenOf(node) {
nodeLevel = getLevel(node);
childrenLevel = nodeLevel + 1;
return $(node).nextUntil("tr[data-" + options.dataAttribute + "=" + nodeLevel + "]", "tr[data-" + options.dataAttribute + "=" + childrenLevel + "]");
};
})(jQuery);