index.vue 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <template>
  2. <a-select
  3. v-model="selected"
  4. :placeholder="placeholder"
  5. :allow-clear="clearable"
  6. :disabled="disabled"
  7. :options="optionList"
  8. allow-search
  9. popup-container="body"
  10. v-bind="attrs"
  11. :trigger-props="{ autoFitPopupMinWidth: true }"
  12. style="width: 335px !important"
  13. @change="onChange"
  14. >
  15. <template v-if="prefix" #prefix>考点</template>
  16. </a-select>
  17. </template>
  18. <script setup lang="ts">
  19. import { ref, useAttrs, watch } from 'vue';
  20. import { agentQuery } from '@/api/order';
  21. defineOptions({
  22. name: 'SelectAgent',
  23. });
  24. type ValueType = number | Array<number> | null;
  25. const props = defineProps<{
  26. modelValue: ValueType;
  27. clearable?: boolean;
  28. disabled?: boolean;
  29. placeholder?: string;
  30. multiple?: boolean;
  31. teachingId: number | null;
  32. prefix?: boolean;
  33. flag?: boolean;
  34. }>();
  35. const emit = defineEmits(['update:modelValue', 'change']);
  36. const attrs = useAttrs();
  37. interface OptionListItem {
  38. value: number;
  39. label: string;
  40. }
  41. const selected = ref<number | Array<number> | undefined>();
  42. const optionList = ref<OptionListItem[]>([]);
  43. const search = async () => {
  44. optionList.value = [];
  45. if (!props.teachingId) return;
  46. const resData = await agentQuery({
  47. id: props.teachingId,
  48. flag: props.flag ? props.flag : undefined,
  49. });
  50. optionList.value = (resData || []).map((item) => {
  51. return { ...item, value: item.id, label: item.name };
  52. });
  53. if (selected.value) {
  54. const targetOption = optionList.value.find(
  55. (item) => item.value === selected.value
  56. );
  57. if (!targetOption) {
  58. emit('update:modelValue', null);
  59. emit('change', null);
  60. }
  61. }
  62. };
  63. const onChange = () => {
  64. const selectedData = props.multiple
  65. ? optionList.value.filter(
  66. (item) =>
  67. selected.value && (selected.value as number[]).includes(item.value)
  68. )
  69. : optionList.value.filter((item) => selected.value === item.value);
  70. emit('update:modelValue', selected.value || null);
  71. emit('change', props.multiple ? selectedData : selectedData[0]);
  72. };
  73. watch(
  74. () => props.modelValue,
  75. (val) => {
  76. selected.value = val || undefined;
  77. },
  78. {
  79. immediate: true,
  80. }
  81. );
  82. watch(
  83. () => props.teachingId,
  84. (val, oldval) => {
  85. if (!val) {
  86. optionList.value = [];
  87. selected.value = undefined;
  88. emit('update:modelValue', selected.value || null);
  89. } else if (val !== oldval) {
  90. search();
  91. }
  92. },
  93. {
  94. immediate: true,
  95. }
  96. );
  97. </script>