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
<template>
<div style="display: inline-block" class="check-box">
<el-checkbox-group v-model="cbVal" @change="handleChange">
<el-checkbox
v-for="(item, index) in checkboxOptions"
:key="index"
:label="item.value"
>{{ item.label }}</el-checkbox>
</el-checkbox-group>
</div>
</template>
<script>
import { apiMap } from '../../fieldConfig/apis'
export default {
name: 'RcCheckbox',
components: {},
model: {
prop: 'value',
event: 'change'
},
props: {
value: {
type: Array,
default: () => []
},
options: {
type: Array,
default() {
return [
{
value: '1',
label: '选项一'
}
]
}
},
optionMode: {
type: String,
default() {
return 'static'
}
},
optionApi: {
type: Object,
default() {
return {}
}
}
},
data() {
return {
cbVal: this.value,
checkboxOptions: []
}
},
computed: {},
watch: {
value(val) {
this.cbVal = val
},
optionMode(val) {
this.value = []
switch (val) {
case 'static': this.checkboxOptions = 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.checkboxOptions = this.options
}
},
mounted() {},
methods: {
handleChange(selectVal) {
this.$emit('change', selectVal)
if (selectVal instanceof Array) {
const selectLabel = []
selectVal.forEach(val => {
// val 为 option 的value
this.checkboxOptions.some(option => {
if (option.value === val) {
selectLabel.push(option.label)
// 终止循环
return true
}
})
})
this.$emit('changeObj', JSON.stringify(selectLabel))
}
},
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 = []
res.data.forEach(item => {
optionArray.push({
label: item.label || item.name,
value: item.value || item.businessId || item.id
})
})
this.checkboxOptions = optionArray
} else {
this.checkboxOptions = []
}
})
}
} else {
}
}
}
}
</script>
<style scoped lang="stylus">
.check-box{
width calc(100% - 75px)
vertical-align middle
}
</style>