餐饮油烟智能监测与监管一体化平台
riku
2026-03-17 b1a0d701cf898c8b7812e66a808a1c91f2bae6cc
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
<template>
  <el-form-item :label="label">
    <el-select
      v-if="optionType == 'select'"
      :model-value="_value"
      @update:model-value="handleChange"
      :placeholder="label"
      :style="'width: ' + width"
    >
      <el-option
        v-for="s in options"
        :key="s.value"
        :label="s.label"
        :value="s.value"
      />
    </el-select>
    <el-checkbox-group
    v-else-if="optionType == 'checkbox'"
      :model-value="_value"
      @update:model-value="handleChange"
      :options="options"
    />
  </el-form-item>
</template>
<script setup>
import { computed, onMounted } from 'vue';
 
const props = defineProps({
  /**
   * 返回结果果,对于不同类型的选择器,数据结构不同
   * select | radio: {label:'', value:''}
   * checkbox: [{label:'', value:''}, ...]
   */
  modelValue: Object,
  // 选项
  options: Array,
  /**
   * 选择器类型
   * select: 下拉框
   * checkbox: 多选框
   * radio:单选框
   */
  optionType: {
    type: String,
    default: 'select'
  },
  label: {
    type: String,
    default: '查询条件'
  },
  // 是否默认返回初始选项
  initValue: {
    type: Boolean,
    default: true
  },
  // form表单绑定属性名
  prop: {
    type: String,
    default: ''
  },
  width: {
    type: String,
    default: '75px'
  }
});
 
const emits = defineEmits(['update:modelValue']);
 
/**
 * 选择器结果,对于不同类型的选择器,数据结构不同
 * select: string | number | boolean
 * checkbox: Array
 * radio: string | number | boolean
 */
const _value = computed(() => {
  switch (props.optionType) {
    case 'select':
    case 'radio':
      return props.modelValue?.value;
    case 'checkbox':
      return props.modelValue?.map((v) => v.value);
    default:
      return props.modelValue?.value;
  }
});
 
function handleChange(value) {
  let opt;
  switch (props.optionType) {
    case 'select':
    case 'radio':
      opt = props.options.find((v) => v.value == value);
      break;
    case 'checkbox':
      opt = props.options.filter((v) => value.indexOf(v.value) != -1);
      break;
    default:
      break;
  }
  emits('update:modelValue', opt);
}
 
onMounted(() => {
  if (props.initValue) {
    switch (props.optionType) {
      case 'select':
      case 'radio':
        emits('update:modelValue', props.options[0]);
        break;
      case 'checkbox':
        emits('update:modelValue', props.options);
        break;
      default:
        break;
    }
  }
});
</script>
<style scoped></style>