index.vue 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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.name"
  14. />
  15. </t-select>
  16. </template>
  17. <script setup name="SelectCustomer">
  18. import { onMounted, ref, useAttrs, watch } from 'vue';
  19. import { customerListApi } 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 search = async () => {
  30. if (props.typeRequired && !props.type) return;
  31. optionList.value = [];
  32. let data = { enable: true };
  33. if (props.type) data.type = props.type;
  34. const res = await customerListApi(data).catch(() => {});
  35. if (!res) return;
  36. optionList.value = res;
  37. };
  38. const onChange = () => {
  39. const isMultiple = Object.prototype.hasOwnProperty.call(attrs, 'multiple');
  40. const selectedData = isMultiple
  41. ? optionList.value.filter(
  42. (item) => selected.value && selected.value.includes(item.id)
  43. )
  44. : optionList.value.filter((item) => selected.value === item.id);
  45. emit('update:modelValue', selected.value);
  46. emit('change', isMultiple ? selectedData : selectedData[0]);
  47. };
  48. onMounted(() => {
  49. search();
  50. });
  51. watch(
  52. () => props.modelValue,
  53. (val) => {
  54. selected.value = val;
  55. }
  56. );
  57. watch(
  58. () => props.type,
  59. (val, oldval) => {
  60. if (val !== oldval) search();
  61. }
  62. );
  63. </script>