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
<template>
<el-select :clearable="clearable" v-model="valueSync" :multiple="multiple" :disabled="disabled" :filterable="filterable" :placeholder="$t('请选择')">
<el-option v-for="item in optionsFormated" :key="item.key" :label="item.label" :value="item.value" />
</el-select>
</template>
<script>
export default {
data(){
return {
valueSync: undefined
}
},
props:{
options: Array,
value: [String, Number, Array, Boolean, Object],
labelField: {
type: [String, Function],
default: 'label'
},
valueField: {
type: String,
default: 'value'
},
keyField:{
type: String,
default: 'value'
},
filterable: Boolean,
multiple: Boolean,
clearable: Boolean,
defaultable: Boolean, // 没有值的时候是否选择第一项
disabled: Boolean,
test: String
},
computed:{
optionsFormated(){
let arr = []
this.options.forEach((item, index) => {
arr.push({
label: typeof this.labelField == 'string' ? item[this.labelField] : (this.labelField)(item, index),
value: item[this.valueField],
key: item[this.keyField]
})
})
return arr
}
},
watch:{
valueSync(val){
console.log('valueSync变化', val, this.value)
this.$emit('input', val)
},
value(value){
console.log('value传值变化', value, this.valueSync)
if(value !== undefined)this.valueSync = value
},
optionsFormated(){
this.setDefault()
},
test(val, old){
console.warn('test变化', val, old)
}
},
created(){
if(this.test == 'allPriceUnit'){
console.log('allPriceUnit selector', this.value, this.defaultable)
}
this.valueSync = this.value
this.setDefault()
},
methods:{
setDefault(){
if(!this.defaultable){
return false
}
if(this.optionsFormated.length && (this.valueSync == null || this.valueSync == '')){
this.valueSync = this.optionsFormated[0].value
}
}
}
}
</script>