89f2ba8b
梁灏
init Tree component
|
1
|
<template>
|
e81207a2
梁灏
update Tree
|
2
3
|
<ul :class="classes">
<li v-for="item in data" :class="itemCls(item)">
|
b923c818
梁灏
update Tree
|
4
5
|
<span :class="arrowCls(item)" @click="setExpand(item.disabled, $index)">
<Icon type="arrow-right-b"></Icon>
|
e81207a2
梁灏
update Tree
|
6
|
</span>
|
b923c818
梁灏
update Tree
|
7
|
<Checkbox
|
b566d106
梁灏
update Tree
|
8
|
v-if="showCheckbox"
|
b923c818
梁灏
update Tree
|
9
10
|
:checked="item.checked && item.childrenCheckedStatus == 2"
:disabled="item.disabled || item.disableCheckbox"
|
07e243ff
梁灏
update Checkbox i...
|
11
|
:indeterminate="item.checked && item.childrenCheckedStatus == 1"
|
b923c818
梁灏
update Tree
|
12
|
@click.prevent="setCheck(item.disabled||item.disableCheckbox,$index)"></Checkbox>
|
e81207a2
梁灏
update Tree
|
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
<a :class="titleCls(item)" @click="setSelect(item.disabled, $index)">
<span :class="[prefixCls + '-title']" v-html="item.title"></span>
</a>
<tree
v-if="!item.isLeaf"
v-show="item.expand"
:class="expandCls(item)"
:data.sync="item.node"
:key="this.key+'.'+$index"
:multiple="multiple"
:show-checkbox="showCheckbox"
transition="slide-up"></tree>
</li>
</ul>
|
89f2ba8b
梁灏
init Tree component
|
27
28
|
</template>
<script>
|
b923c818
梁灏
update Tree
|
29
30
|
import Icon from '../icon/icon.vue';
import Checkbox from '../checkbox/checkbox.vue';
|
e81207a2
梁灏
update Tree
|
31
32
33
34
|
import { t } from '../../locale';
const prefixCls = 'ivu-tree';
|
89f2ba8b
梁灏
init Tree component
|
35
|
export default {
|
e81207a2
梁灏
update Tree
|
36
|
name: 'tree',
|
b923c818
梁灏
update Tree
|
37
|
components: { Icon, Checkbox },
|
e81207a2
梁灏
update Tree
|
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
|
props: {
data: {
type: Array,
default () {
return [];
}
},
key: {
type: String,
default: '0'
},
multiple: {
type: Boolean,
default: false
},
showCheckbox: {
type: Boolean,
default: false
},
onSelect: {
type: Function,
default () {
return {};
}
},
onCheck: {
type: Function,
default () {
return {};
}
},
emptyText: {
type: String,
default () {
return t('i.tree.emptyText');
}
}
},
|
89f2ba8b
梁灏
init Tree component
|
76
|
data () {
|
e81207a2
梁灏
update Tree
|
77
78
79
|
return {
prefixCls: prefixCls
};
|
89f2ba8b
梁灏
init Tree component
|
80
|
},
|
e81207a2
梁灏
update Tree
|
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
|
computed: {
classes () {
if (this.key === '0') {
return this.prefixCls;
} else {
return `${this.prefixCls}-child-tree`;
}
}
},
watch: {
data(){
if (this.key === '0') {
this.setKey();
this.preHandle();
}
}
},
methods: {
itemCls (item) {
return [
{
[`${prefixCls}-item-disabled`]: item.disabled
}
];
},
arrowCls (item) {
return [
`${this.prefixCls}-switcher`,
{
[`${this.prefixCls}-switcher-disabled`]: item.disabled,
[`${this.prefixCls}-noline_close`]: !item.expand && !item.isLeaf,
[`${this.prefixCls}-noline_open`]: item.expand && !item.isLeaf,
[`${this.prefixCls}-switcher-noop`]: item.isLeaf
}
];
},
|
e81207a2
梁灏
update Tree
|
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
|
titleCls (item) {
return [
{
[`${this.prefixCls}-node-selected`]: item.selected
}
];
},
expandCls (item) {
return [
{
[`${this.prefixCls}-child-tree-open`]: item.expand
}
];
},
setKey () {
for (let i = 0; i < this.data.length; i++) {
this.data[i].key = `${this.key}.${i}`;
}
},
|
b923c818
梁灏
update Tree
|
136
|
preHandle () {
|
e81207a2
梁灏
update Tree
|
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
|
for (let [i,item] of this.data.entries()) {
if (!item.node || !item.node.length) {
this.$set(`data[${i}].isLeaf`, true);
this.$set(`data[${i}].childrenCheckedStatus`, 2);
continue;
}
if (item.checked && !item.childrenCheckedStatus) {
this.$set(`data[${i}].childrenCheckedStatus`, 2);
this.$broadcast('parentChecked', true, `${this.key}.${i}`);
} else {
let status = this.getChildrenCheckedStatus(item.node);
this.$set(`data[${i}].childrenCheckedStatus`, status);
if (status !== 0) this.$set(`data[${i}].checked`, true);
}
}
},
|
b923c818
梁灏
update Tree
|
153
|
setExpand (disabled, index) {
|
e81207a2
梁灏
update Tree
|
154
155
156
157
|
if (!disabled) {
this.$set(`data[${index}].expand`, !this.data[index].expand);
}
},
|
b923c818
梁灏
update Tree
|
158
|
setSelect (disabled, index) {
|
e81207a2
梁灏
update Tree
|
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
|
if (!disabled) {
const selected = !this.data[index].selected;
if (this.multiple || !selected) {
this.$set(`data[${index}].selected`, selected);
} else {
for (let i = 0; i < this.data.length; i++) {
if (i == index) {
this.$set(`data[${i}].selected`, true);
} else {
this.$set(`data[${i}].selected`, false);
}
}
}
this.$dispatch('nodeSelected', this, selected);
}
},
|
b923c818
梁灏
update Tree
|
175
|
setCheck (disabled, index) {
|
e81207a2
梁灏
update Tree
|
176
177
178
179
180
181
182
|
if (disabled) return;
const checked = !this.data[index].checked;
this.$set(`data[${index}].checked`, checked);
this.$set(`data[${index}].childrenCheckedStatus`, checked ? 2 : 0);
this.$dispatch('childChecked', this, this.key);
this.$broadcast('parentChecked', checked, `${this.key}.${index}`);
},
|
b923c818
梁灏
update Tree
|
183
|
getNodes (data, opt) {
|
e81207a2
梁灏
update Tree
|
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
|
data = data || this.data;
let res = [];
for (let node of data) {
let tmp = true;
for (let [key, value] of Object.entries(opt)) {
if (node[key] != value) {
tmp = false;
break;
}
}
if (tmp) {
res.push(node);
}
if (node.node && node.node.length) {
res = res.concat(this.getNodes(node.node, opt));
}
}
return res;
},
|
b923c818
梁灏
update Tree
|
203
|
getSelectedNodes () {
|
e81207a2
梁灏
update Tree
|
204
205
|
return this.getNodes(this.data, {selected: true});
},
|
b923c818
梁灏
update Tree
|
206
|
getCheckedNodes () {
|
e81207a2
梁灏
update Tree
|
207
208
|
return this.getNodes(this.data, {checked: true, childrenCheckedStatus: 2});
},
|
b923c818
梁灏
update Tree
|
209
|
getChildrenCheckedStatus (children) {
|
e81207a2
梁灏
update Tree
|
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
|
let checkNum = 0, child_childrenAllChecked = true;
for (let child of children) {
if (child.checked) {
checkNum++;
}
if (child.childrenCheckedStatus !== 2) {
child_childrenAllChecked = false;
}
}
// select all
if (checkNum == children.length) {
return child_childrenAllChecked ? 2 : 1;
// select some
} else if (checkNum > 0) {
return 1;
} else {
return 0;
}
}
},
ready(){
this.setKey();
this.preHandle();
this.$on('nodeSelected', (ori, selected) => {
if (this.key !== '0') return true;
if (!this.multiple && selected) {
if (this !== ori) {
for (let i = 0; i < this.data.length; i++) {
this.$set(`data[${i}].selected`, false);
}
}
this.$broadcast('cancelSelected', ori);
}
if (this.onSelect) {
this.$nextTick(() => {
this.onSelect(this.getSelectedNodes());
});
}
});
this.$on('cancelSelected', ori => {
this.$broadcast('cancelSelected', ori);
if (this !== ori) {
for (let i = 0; i < this.data.length; i++) {
this.$set(`data[${i}].selected`, false);
}
}
});
this.$on('parentChecked', (status, key) => {
if (this.key == key || this.key.startsWith(key + '.')) {
for (let i = 0; i < this.data.length; i++) {
this.$set(`data[${i}].checked`, status);
this.$set(`data[${i}].childrenCheckedStatus`, status ? 2 : 0);
}
this.$broadcast('parentChecked', status, key);
}
});
this.$on('childChecked', (ori, key) => {
if (this.key === '0' && this.onCheck) {
this.$nextTick(() => {
this.onCheck(this.getCheckedNodes());
});
}
if (this === ori) return;
for (let [i,item] of this.data.entries()) {
if (this.key + '.' + i == key) {
let temp = this.getChildrenCheckedStatus(item.node);
if (temp != item.childrenCheckedStatus) {
this.$set(`data[${i}].checked`, !!temp);
this.$set(`data[${i}].childrenCheckedStatus`, temp);
if (this.key !== '0') this.$dispatch('childChecked', this, this.key);
}
}
}
});
}
|
89f2ba8b
梁灏
init Tree component
|
286
287
|
};
</script>
|