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
<template>
<ion-page>
<ion-header>
<ion-toolbar v-show="titleStatus">
<ion-buttons slot="start">
<ion-button @click="closeView">
<ion-icon name="chevron-back"></ion-icon>
{{ $t('app.button.back') }}
</ion-button>
</ion-buttons>
<ion-title
><label class="title-label">{{ $t('app.title.choose') }}</label></ion-title
>
</ion-toolbar>
</ion-header>
<div class="app-tree">
<div class="app-tree-content">
<div class="app-tree__treenav">
<template v-for="(item, index) in treeNav">
<span
:key="item.id"
:class="index + 1 < treeNav.length ? 'app-tree__treenav--active' : ''"
@click="nav_click(item)"
>{{ item.label }}</span
>
<span class="app-tree__treenav__text" :key="item.id + 'span'" v-if="index + 1 < treeNav.length">></span>
</template>
</div>
<div class="app-tree__partition" v-if="valueNodes.length > 0"></div>
<ion-list class="app-tree__list">
<template v-for="(item, index) in renderData">
<ion-item class="app-tree__list__item" :key="index" @click="click_node(item)">
<ion-label
>{{ item.label
}}<span v-if="item.children && item.children.length > 0">
({{ item.children.length }})</span
></ion-label
>
<app-mob-icon
class="app-tree__icon "
position="end"
:name="geticon(item.status)"
@onClick="icon_click(item)"
></app-mob-icon>
</ion-item>
</template>
</ion-list>
</div>
<div class="app-tree__footer">
<app-mob-button class="app-tree__footer__button" @click="on_ok_click">{{ $t('app.button.confirm') }}</app-mob-button>
</div>
</div>
</ion-page>
</template>
<script lang="ts">
import { Vue, Component, Prop, Watch } from 'vue-property-decorator';
@Component({
components: {},
i18n: {
messages: {
'ZH-CN': {
choose: '选择',
},
'EN-US': {
choose: 'Choose',
},
},
},
})
export default class AppTree extends Vue {
/**
* 选择项
*
* @type {string}
* @memberof AppTree
*/
public selectTreeValue?: string;
@Prop() public dynamicProps?: string;
@Watch('dynamicProps', { immediate: true })
on_dynamicProps_change(newVal: any, oldVal: any) {
const _context = newVal._context;
let _viewparams: any;
if (newVal._viewparams) {
_viewparams = typeof newVal._viewparams == 'string' ? JSON.parse(newVal._viewparams) : newVal._viewparams;
}
const _this: any = this;
if (!_viewparams) {
return;
}
for (let index = 0; index < Object.keys(_viewparams).length; index++) {
const item = Object.keys(_viewparams)[index];
_this[item] = _viewparams[item];
}
}
@Watch('selectTreeValue', { immediate: true })
on_selectTreeValue_change(newVale: any, oldVal: any) {
this.cacheValue = newVale ? JSON.parse(newVale) : [];
this.curValue = newVale ? JSON.parse(newVale) : [];
}
/**
* 缓存值
*
* @type {any[]}
* @memberof AppTree
*/
public cacheValue: any = [];
/**
* 标题显示状态
*
* @type {any[]}
* @memberof AppTree
*/
public titleStatus: boolean = true;
/**
* 设置工具栏状态
*
* @memberof IbzDailyDailyCreateMobEditViewBase
*/
public setViewTitleStatus() {
const thirdPartyName = this.$store.getters.getThirdPartyName();
if (thirdPartyName) {
this.titleStatus = false;
}
}
/**
* 当前选中值
*
* @type {any[]}
* @memberof AppTree
*/
public curValue: any[] = [];
/**
* 是否多选
*/
public multiple?: boolean;
/**
* 父节点数据
*/
public rootNodes: any = [];
/**
* 子节点数据
*/
public valueNodes: any = [];
/**
* 树导航栏数据
*/
public treeNav: any[] = [{ id: 'main', label: 'main' }];
/**
* 获取图标
*/
public geticon(value: any) {
switch (value) {
case 0:
return 'ellipse-outline';
case 1:
return 'checkmark-circle-outline';
case 2:
return 'remove-circle-outline';
default:
'ellipse-outline';
break;
}
}
/**
* 树数据
*/
public nodesData?: any[];
/**
* nodesData值变化
*/
@Watch('nodesData', { immediate: true, deep: true })
on_nodesData_change(newVale: any, oldVal: any) {
if (!newVale) {
return;
}
this.parseNodes(newVale, true);
}
/**
* 渲染数据
*/
public renderData: any = [];
/**
* 解析节点数据
*
* @param {*} nodes
* @memberof EmpTreeBase
*/
public parseNodes(nodes: any, isRender: boolean = false) {
if (isRender) {
this.renderData = [];
}
for (let index = 0; index < nodes.length; index++) {
const node: any = nodes[index];
if (isRender) {
this.renderData.push(Object.assign({ status: this.getSelectStatus(node) }, node));
}
}
}
/**
* 设置状态属性
* 0未选择
* 1选择
* 部分选择
* @param {*} nodes
* @memberof EmpTreeBase
*/
public getSelectStatus(node: any): number {
if (this.curValue.length <= 0) {
return 0;
}
if (node.children?.length > 0) {
let temp_arr = [];
if (!node.children) {
return 0;
}
for (let index = 0; index < node.children.length; index++) {
const node_item = node.children[index];
temp_arr.push(this.getSelectStatus(node_item));
}
if (
temp_arr.every((item: any) => {
return Object.is(item, 1);
})
) {
return 1;
}
if (
temp_arr.every((item: any) => {
return Object.is(item, 0);
})
) {
return 0;
}
if (
temp_arr.some((item: any) => {
return Object.is(item, 1);
}) ||
temp_arr.some((item: any) => {
return Object.is(item, 2);
})
) {
return 2;
}
} else {
return this.curValue?.some((item: any) => {
return Object.is(item.id, node.id);
})
? 1
: 0;
}
return 0;
}
/**
* 导航点击
*/
public nav_click(item: any) {
if (item.id == this.treeNav[this.treeNav.length - 1].id) {
return;
}
debugger;
const count = this.treeNav.findIndex((i: any) => item.id === i.id) + 1;
this.treeNav.splice(count, this.treeNav.length - 1);
if (Object.is(item.id, 'main')) {
this.parseNodes(this.nodesData, true);
} else {
this.click_node(item, false);
// this.parseNodes(item, true)
}
}
/**
* 节点点击
*/
public click_node(item: any, isSetNav: boolean = true) {
if (item.children?.length > 0) {
if (isSetNav) this.treeNav.push(item);
this.parseNodes(item.children, true);
return;
}
this.icon_click(item);
}
/**
* 图标点击
*/
public icon_click(item: any) {
if (this.multiple) {
const _this = this;
const node_index = this.curValue.findIndex((_item: any) => {
return Object.is(item.id, _item.id);
});
let temp_allNode = this.getNodeChild(item);
if (node_index == -1) {
item.status = 1;
}
for (let index = 0; index < temp_allNode.length; index++) {
const temp_node = temp_allNode[index];
if (node_index > -1) {
const _index = _this.curValue.findIndex((_item: any) => {
return Object.is(temp_node.id, _item.id);
});
_this.curValue.splice(_index, 1);
} else {
if (
this.curValue.findIndex((_item: any) => {
return Object.is(_item.id, temp_node.id);
}) == -1
) {
this.curValue.push(temp_node);
}
}
}
} else {
this.curValue = [item];
}
let currNav = this.treeNav[this.treeNav.length - 1];
this.parseNodes(Object.is(currNav.id, 'main') ? this.nodesData : currNav.children, true);
this.$forceUpdate();
}
/**
* 获取当前节点所有子节点 包括当前节点
*/
public getNodeChild(node: any): any[] {
let allchildren: any[] = [];
allchildren.push(node);
if (node.children) {
for (let index = 0; index < node.children.length; index++) {
const element = node.children[index];
if (element.children && element.children.length > 0) {
this.getNodeChild(element)?.forEach((item: any) => {
allchildren.push(item);
});
} else {
allchildren.push(element);
}
}
}
return allchildren;
}
/**
* on_ok_click
*/
public on_ok_click() {
this.$emit('close', JSON.parse(JSON.stringify(this.curValue)));
}
/**
* 生命周期
*/
public created() {
this.setViewTitleStatus();
}
/**
* 第三方容器初始化
*
* @memberof IbzDailyDailyCreateMobEditViewBase
*/
protected thirdPartyInit() {
// this.$viewTool.setViewTitleOfThirdParty(this.$t('choose'));
// this.$viewTool.setThirdPartyEvent(this.closeView);
}
/**
* 关闭视图
*/
public closeView() {
this.$emit('close');
}
}
</script>