|
|
<template> <div> <el-select v-bind="$attrs" v-on="$listeners" :value="value" style="width: 100%" @change="changeDictData"> <el-option v-for="(item, index) in options" :key="index" :label="item.dictLabel" :value="item.dictValue" :disabled="item.status === 'N' || disabledOptionsValue.includes(item.dictValue)"> <slot :label="item.dictLabel" :value="item.dictValue"></slot> </el-option> </el-select> </div></template><script>import { selectDictDataList, selectDictTypeList } from '../../../api/dict'export default { name: 'dictDataSelect', model: { prop: 'value', event: 'change' }, props: { dictType: { type: String }, dictTypeList: { type: Array, default: () => [] }, value: { required: true }, useDefaultValue: { type: Boolean, default: true }, useTypeMultiDefaultValue: { type: Boolean, default: false }, useSite: { type: Boolean, default: true }, disabledOptionsValue: { type: Array, default: () => [] }, site: { type: String, default: undefined }, siteRequired: { type: Boolean, default: false } }, data () { return { options: [], dictTypeMultiDefaultValue: '' } }, watch: { dictType () { this.initOption() }, site () { this.initOption() } }, created () { this.initOption() }, methods: { hasCurrentValue () { if (Array.isArray(this.value)) { return this.value.length > 0 } return this.value !== null && this.value !== undefined && String(this.value).trim() !== '' }, isMultipleMode () { if (Array.isArray(this.value)) { return true } const multiple = this.$attrs ? this.$attrs.multiple : false if (multiple === '' || multiple === true || multiple === 'multiple') { return true } if (typeof multiple === 'string') { return multiple !== 'false' } return !!multiple }, parseMultiDefaultValue (value) { if (value === null || value === undefined) { return [] } const cache = new Set() return String(value) .split(/[;,\n]/) .map(item => String(item || '').trim()) .filter(item => { if (!item || cache.has(item)) { return false } cache.add(item) return true }) }, async loadDictTypeMultiDefaultValue () { this.dictTypeMultiDefaultValue = '' const currentDictType = String(this.dictType || '').trim() if (!this.useTypeMultiDefaultValue || !currentDictType) { return } const site = this.resolveQuerySite() if (!site) { return } try { let { data } = await selectDictTypeList({ site, dictType: currentDictType }) if (data && data.code === 0 && Array.isArray(data.rows)) { const matched = data.rows.find(item => String(item.dictType || '').trim() === currentDictType) this.dictTypeMultiDefaultValue = matched ? String(matched.multiDefaultValue || '').trim() : '' } } catch (e) {} }, dictDefaultValue () { if (this.hasCurrentValue() || this.options.length === 0) { return } if (this.isMultipleMode()) { const enabledValueSet = new Set( this.options.filter(item => item.status === 'Y').map(item => item.dictValue) ) let defaultValues = this.parseMultiDefaultValue(this.dictTypeMultiDefaultValue) .filter(item => enabledValueSet.has(item)) if (defaultValues.length === 0) { defaultValues = this.options .filter(item => item.isDefault === 'Y' && item.status === 'Y') .map(item => item.dictValue) } if (defaultValues.length > 0) { this.$emit('change', defaultValues) } return } let find = this.options.find(item => item.isDefault === 'Y') if (find && find.status === 'Y') { this.$emit('change', find.dictValue) } }, resolveQuerySite () { const explicitSite = String(this.site || '').trim() if (this.siteRequired) { return explicitSite } if (explicitSite) { return explicitSite } return this.useSite ? this.$store.state.user.site : '*' }, async initOption () { const requestId = (this.dictRequestId || 0) + 1 this.dictRequestId = requestId const site = this.resolveQuerySite() if (this.siteRequired && !String(site || '').trim()) { this.options = [] return } let params = { site, dictType: this.dictType, dictTypeList: this.dictTypeList }
try { let { data } = await selectDictDataList(params) if (requestId !== this.dictRequestId) { return } if (data && data.code === 0) { this.options = data.rows if (this.options.length === 0) { this.$emit('change', this.isMultipleMode() ? [] : '') } } await this.loadDictTypeMultiDefaultValue() if (requestId !== this.dictRequestId) { return } if (this.useDefaultValue) { this.dictDefaultValue() } } catch (e) { if (requestId !== this.dictRequestId) { return } return Promise.reject(e) } }, changeDictData (val) { this.$emit('change', val) } }}</script>
|