index.vue 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <template>
  2. <t-select
  3. v-model="selected"
  4. filterable
  5. clearable
  6. v-bind="attrs"
  7. @change="onChange"
  8. >
  9. <t-option
  10. v-for="item in optionList"
  11. :key="item.id"
  12. :value="item.id"
  13. :label="item.level"
  14. />
  15. </t-select>
  16. </template>
  17. <script setup name="SelectServiceLevel">
  18. import { onMounted, ref, useAttrs, watch, computed } from 'vue';
  19. import { serviceLevelListApi } from '@/api/system';
  20. let optionList = ref([]);
  21. let selected = ref('');
  22. const attrs = useAttrs();
  23. const emit = defineEmits(['update:modelValue', 'change']);
  24. const props = defineProps({
  25. modelValue: { type: [Number, String, Array], default: '' },
  26. type: { type: String, default: '' },
  27. typeRequired: { type: Boolean, default: false },
  28. });
  29. const isMultiple = computed(() => {
  30. const multiple = attrs.multiple;
  31. return multiple === '' || multiple;
  32. });
  33. const search = async () => {
  34. if (props.typeRequired && !props.type) return;
  35. optionList.value = [];
  36. let data = { enable: true };
  37. if (props.type) data.type = props.type;
  38. const res = await serviceLevelListApi(data).catch(() => {});
  39. if (!res) return;
  40. optionList.value = res;
  41. };
  42. const onChange = () => {
  43. const selectedData = isMultiple.value
  44. ? optionList.value.filter(
  45. (item) => selected.value && selected.value.includes(item.id)
  46. )
  47. : optionList.value.filter((item) => selected.value === item.id);
  48. emit('update:modelValue', selected.value);
  49. emit('change', isMultiple.value ? selectedData : selectedData[0]);
  50. };
  51. onMounted(() => {
  52. search();
  53. });
  54. watch(
  55. () => props.modelValue,
  56. (val) => {
  57. selected.value = val;
  58. }
  59. );
  60. watch(
  61. () => props.type,
  62. (val, oldval) => {
  63. if (val !== oldval) {
  64. search();
  65. emit('update:modelValue', null);
  66. emit('change', isMultiple.value ? [] : null);
  67. }
  68. }
  69. );
  70. </script>