-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathng-checkbox-tree.directive.js
127 lines (120 loc) · 5.62 KB
/
ng-checkbox-tree.directive.js
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
/**
* ngCheckboxTree Module
*
*/
(function() {
'use strict';
angular.module('ngCheckboxTree', []).directive('ngCheckboxTree', function($compile) {
return {
name: 'tuSimpleTree',
scope: {
treeData: '=',
treeSelected: '='
},
require: '?^ngModel',
restrict: 'E',
replace: 'true',
template: '<div class="ng-checkbox-tree"></div>',
transclude: false,
link: function(scope, ele, attrs, controller) {
console.log(attrs);
var indexTree = angular.element(ele);
//监控treeDtata
scope.$watch('treeData', function() {
removeBranches(indexTree);
createBranches(scope.treeData, indexTree);
if (attrs.leafOnly === "on") {
leafOnly();
}
//
indexTree.find('input').bind('change', function() {
select(angular.element(this));
});
}, true);
//递归地创建分支
function createBranches(data, treeNode) {
if (data.length > 0) {
var treeBase = treeNode.append('<ul></ul>').children('ul');
for (var i = 0; i < data.length; i++) {
var newTreeNode = treeBase.append('<li><input type="checkbox" value=' + data[i].id + '/>' + data[i].title + '</li>').children('li:eq(' + i + ')');
createBranches(data[i].nodes, newTreeNode);
}
}
}
function leafOnly () {
console.log("ooooooooooooooooo");
indexTree.find('li').each(function () {
var self = angular.element(this);
var isLeaf = (self.children('ul').length === 0);
if (!isLeaf) {
self.children('input').remove();
}
}) ;
}
//移除分支
function removeBranches(treeNode) {
treeNode.empty();
}
function select(checkbox) {
var currentUl = checkbox.parent().parent();
var currentsLi = currentUl.children('li');
var childrenCheckbox = checkbox.parent().find('input');
if (attrs.treeLinkage === "on") {
linkage ();
}
//check数据导出
var checkSet = indexTree.find('input');
scope.treeSelected = [];
checkSet.each(function() {
if (angular.element(this).is(':checked')) {
scope.treeSelected.push(angular.element(this).val());
}
});
scope.$apply();
//若一个复选框选中,且同级复选框都为选中状态,父级复选框自动选中,此行为递归至条件不满足为止
function linkage () {
if (checkbox.is(':checked')) {
//若一个复选框选中,它的所有子选框自动选中;
childrenCheckbox.each(function() {
angular.element(this).prop('checked', true);
});
//
selectParents(currentsLi);
} else {
//若一个复选框从选中状态切换到没有选中,则为其所有子选框取消选中状态。
childrenCheckbox.each(function() {
angular.element(this).prop('checked', false);
});
cancelParents(currentsLi);
}
function selectParents(currentsLi) {
var currentAllSelected = true;
currentsLi.each(function() {
if (!angular.element(this).children('input').is(':checked')) {
currentAllSelected = false;
}
});
if (currentAllSelected) {
var parentLi = currentsLi.parent().parent();
var parentsLi = currentsLi.parent().parent().parent().children('li');
if (parentLi.length > 0) {
parentLi.children('input').prop('checked', true);
selectParents(parentsLi);
}
}
}
function cancelParents(currentsLi) {
//若一个复选框取消选中,父级复选框递归取消选中
var parentLi = currentsLi.parent().parent();
var parentsLi = currentsLi.parent().parent().parent().children('li');
if (parentLi.length > 0) {
parentLi.children('input').prop('checked', false);
cancelParents(parentsLi);
}
}
}
}
}
};
});
})();