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
<template>
<section class="rc-tree">
<el-tree
:data="treeOptions"
:show-checkbox="showCheckbox"
:props="defaultProps"
@check-change="handleCheckChange"
/>
</section>
</template>
<script>
import { apiMap } from '../../fieldConfig/apis'
export default {
name: 'RcTree',
components: {},
model: {
prop: 'value',
event: 'change'
},
props: {
value: {
type: Array,
default: () => []
},
options: {
type: Array,
default() {
return [
{
value: '1',
label: '选项一'
}
]
}
},
defaultProps: {
type: Object,
default() {
return {
children: 'children',
label: 'label'
}
}
},
showCheckbox: {
type: Boolean,
default: true
},
optionMode: {
type: String,
default() {
return 'static'
}
},
optionApi: {
type: Object,
default() {
return {}
}
}
},
data() {
return {
treeOptions: []
}
},
computed: {},
watch: {
optionMode(val) {
this.value = []
switch (val) {
case 'static': this.treeOptions = this.options; return
case 'origin': this.originOptions(this.optionApi); return
}
},
optionApi: {
deep: true,
handler(val) {
this.value = []
this.originOptions(val)
}
}
},
created() {},
beforeMount() {
if (this.optionMode === 'origin') {
this.originOptions(this.optionApi)
} else {
this.treeOptions = this.options
}
},
mounted() {
},
methods: {
handleCheckChange(data, checked, indeterminate) {
},
originOptions(config) {
if (config.mode === 'builtIn') {
if (config.builtIn === '') {
this.$message('请配置内置api接口')
} else {
apiMap[config.builtIn](this, '', (res) => {
if (res.data instanceof Array) {
const optionArray = []
const idMapper = {}
res.data.forEach(item => {
idMapper[item.businessId] = {
id: item.businessId,
label: item.name,
children: []
}
})
res.data.forEach(item => {
if (item.parentId === undefined) {
optionArray.push(idMapper[item.businessId])
} else {
if (idMapper[item.parentId]) {
idMapper[item.parentId].children.push(idMapper[item.businessId])
}
}
})
this.treeOptions = optionArray
} else {
this.treeOptions = []
}
})
}
// eslint-disable-next-line no-empty
} else {
}
}
}
}
</script>
<style lang="stylus">
.rc-tree {
}
</style>