Files
foton-laws-system-front/src/components/CustomFormComponents/SelectArray.vue
T
2021-12-06 10:34:50 +08:00

137 lines
3.0 KiB
Vue

<template>
<el-col :span="span">
<el-form-item
:label="config.attrName"
:prop="config.attrField"
label-width="150px"
class="add-form-item"
:class="{'form-item-disabled': disabled}"
>
<el-select
v-model="valueArr"
:placeholder="placeholder"
filterable
:disabled="disabled"
clearable
multiple
@change="handleChange"
>
<el-option value="全选" @click.native="clickOption" v-if="options.length > 0">
{{ checkAll ? '取消全选' : '全选' }}
</el-option>
<el-option
v-for="opt in options"
:key="opt.value"
:value="opt.value"
:label="opt.label"
>
{{ opt.label }}
</el-option>
</el-select>
</el-form-item>
</el-col>
</template>
<script>
export default {
name: 'CustomSelectArray',
props: {
config: {
type: Object,
required: true
},
value: {
required: true
},
disabled: {
type: Boolean,
default: false
},
// 栅格比例
span: {
type: Number,
default: 24
}
},
data () {
return {
valueArr: [],
checkAll: false,
}
},
computed: {
placeholder () {
return `请输入${this.config.attrName}`
},
showMessage () {
return `${this.config.attrName}不能为空`
},
isRequired () {
return !!this.config.isMust
},
options () {
return this.config.options || []
}
},
methods: {
clickOption () {
this.checkAll = !this.checkAll
if (this.checkAll) {
let list = []
this.config.options.map(item => {
this.valueArr.push(item.value)
list.push(item.value)
})
this.$emit('input', list)
} else {
this.valueArr = []
this.$emit('input', this.valueArr)
}
},
handleChange (event) {
if (event.includes('全选')) {
return
} else {
if (event.length === this.config.options.length) {
this.checkAll = true
} else {
this.checkAll = false
}
this.$emit('input', event)
}
// const value = event.target.value
}
},
watch: {
value: {
deep: true,
handler (val) {
if (this.value !== '' && this.value !== null && typeof (this.value) !== 'undefined') {
if (this.value instanceof Array) {
this.valueArr = this.value
} else {
this.valueArr = this.value.split(',')
}
} else if (this.value === '') {
this.valueArr = []
}
}
},
},
mounted () {
if (this.value !== '' && this.value !== null && typeof (this.value) !== 'undefined') {
if (this.value instanceof Array) {
this.valueArr = this.value
} else {
this.valueArr = this.value.split(',')
}
}
}
}
</script>
<style scoped>
</style>