-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
277 lines (272 loc) · 9.78 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
*{
margin: 0;
padding: 0;
}
li{
list-style: none;
padding: 10px;
}
.arrow{
cursor: pointer;
display: inline-block;
transition: transform .3s;
}
.arrowTransform{
transform: rotate(90deg);
}
.selected{
background: #2b85e4;
}
</style>
<script src="https://unpkg.com/vue/dist/vue.js"></script>
</head>
<body>
<div id="app">
<my-self-tree
:tree-data="treeData"
:flat-tree="flatTree">
</my-self-tree>
</div>
</body>
<script>
Vue.component("my-self-tree", {
props: ["treeData", "flatTree"],
data() {
return {
}
},
template: `
<ul>
<li v-for="(item, index) in treeData" :key="index">
<span
:class="{arrowTransform: item.expand, arrow: true}"
@click="handleExpand(item)"
v-show="item.children && item.children.length">></span>
<input type="checkbox" v-model="item.checked" @change="handleCheck(item)"/>
<span :class="{selected: item.selected}" @click="handleSelect(item)">{{item.label}}</span>
<transition name="fade">
<my-self-tree
v-show="item.expand"
:tree-data="item.children"
:flat-tree="flatTree">
</my-self-tree>
</transition>
</li>
</ul>
`,
created: function() {
},
methods: {
//展开或收起
handleExpand(item) {
console.log(item)
var expand = item.expand,
nodeKey = item.nodeKey;
var node = this.flatTree[nodeKey].node;
this.$set(node, "expand", !expand);
},
//节点选中
handleSelect(item) {
var nodeKey = item.nodeKey;
var node = this.flatTree[nodeKey].node;
var currentSelectedKey = this.flatTree.findIndex(obj => {
return obj.node.selected;
});
//每次只能选择一个
if(currentSelectedKey >= 0 && currentSelectedKey != item.nodeKey) {
this.$set(this.flatTree[currentSelectedKey].node, 'selected', false);
}
this.$set(node, 'selected', !node.selected);
this.getSelectNodes();
},
//节点勾选
handleCheck(item) {
var checked = item.checked,
nodeKey = item.nodeKey;
var node = this.flatTree[nodeKey].node;
this.$set(node, 'checked', checked);
this.updateTreeUp(nodeKey);
this.updateTreeDown(node, { checked: checked});
this.getCheckedNodes();
},
//判断当前勾选节点的父节点是否需要勾选
updateTreeUp(nodeKey) {
var parentKey = this.flatTree[nodeKey].parent;
var childrenKey = "children";
if (typeof parentKey == 'undefined') return;
var node = this.flatTree[nodeKey].node;
var parent = this.flatTree[parentKey].node;
if (node.checked == parent.checked) return;
if (node.checked == true) {
this.$set(parent, 'checked', parent[childrenKey].every(function (node) {
return node.checked;
}));
} else {
this.$set(parent, 'checked', false);
}
this.updateTreeUp(parentKey);
},
//判断当前勾选节点的子节点是否勾选
updateTreeDown(node) {
var changes = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var childrenKey = "children";
for (var key in changes) {
this.$set(node, key, changes[key]);
}
if (node[childrenKey]) {
node[childrenKey].forEach(function (child) {
this.updateTreeDown(child, changes);
}.bind(this));
}
},
//获取已勾选节点
getCheckedNodes() {
var arr = this.flatTree.filter(item => {
return item.node.checked;
}).map(item => {
return item.node;
});
console.log(arr)
},
//获取已选中节点
getSelectNodes() {
var arr = this.flatTree.filter(item => {
return item.node.selected;
}).map(item => {
return item.node;
});
console.log(arr)
}
}
})
new Vue({
el: "#app",
data: {
treeData: [],
flatTree: []
},
created: function() {
//模拟请求数据
setTimeout(() => {
var arr = [
{
label: '一级 1',
children: [
{
label: '二级 1-1',
children: [
{
label: '三级 1-1-1',
children: [
{
label: '四级 1-1-1',
}
]
}
]
}
]
},
{
label: '一级 2',
children: [
{
label: '二级 2-1',
children: [
{
label: '三级 2-1-1',
}
]
}, {
label: '二级 2-2',
children: [
{
label: '三级 2-2-1',
}
]
}
]
},
{
label: '一级 3',
children: [
{
label: '二级 3-1',
children: [
{
label: '三级 3-1-1',
}
]
},
{
label: '二级 3-2',
children: [
{
label: '三级 3-2-1',
}
]
}
]
}
];
this.treeData = this.compileTreeData(arr);
this.flatTree = this.compileFlatTree(arr);
},2000);
},
methods: {
//数据初始化(添加某些必要属性)
compileTreeData(arr) {
function newAttr(node, parent) {
node.expand = false;//是否展开
node.checked = false;//是否勾选
node.selected = false;//是否选中
if(node.children) {
node.children.forEach(child => {
return newAttr(child, node);
})
}
}
arr.forEach(item => {
newAttr(item);
});
return arr;
},
//编制各节点对应关系
compileFlatTree(arr) {
var keyCounter = 0;
var childrenKey = "children";
var flatTree = [];
function flattenChildren(node, parent) {
node.nodeKey = keyCounter++;
flatTree[node.nodeKey] = {
node: node,
nodeKey: node.nodeKey
};
if (typeof parent != 'undefined') {
flatTree[node.nodeKey].parent = parent.nodeKey;
flatTree[parent.nodeKey][childrenKey].push(node.nodeKey);
}
if (node[childrenKey]) {
flatTree[node.nodeKey][childrenKey] = [];
node[childrenKey].forEach(function (child) {
return flattenChildren(child, node);
});
}
}
arr.forEach(function (rootNode) {
flattenChildren(rootNode);
});
return flatTree;
}
}
})
</script>
</html>