index.vue 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <template>
  2. <a-select
  3. v-model="selected"
  4. :placeholder="placeholder"
  5. :allow-clear="clearable"
  6. :disabled="disabled"
  7. :options="optionList"
  8. filter-option
  9. v-bind="attrs"
  10. @change="onChange"
  11. >
  12. <template v-if="prefix" #prefix>考点</template>
  13. </a-select>
  14. </template>
  15. <script setup lang="ts">
  16. import { ref, useAttrs, watch } from 'vue';
  17. import { agentQuery } from '@/api/order';
  18. defineOptions({
  19. name: 'SelectAgent',
  20. });
  21. const props = defineProps<{
  22. modelValue: number | string;
  23. clearable?: boolean;
  24. disabled?: boolean;
  25. placeholder?: string;
  26. multiple?: boolean;
  27. teachingId: number | string;
  28. prefix?: boolean;
  29. }>();
  30. const emit = defineEmits(['update:modelValue', 'change']);
  31. const attrs = useAttrs();
  32. interface OptionListItem {
  33. value: number;
  34. label: string;
  35. }
  36. const selected = ref('');
  37. const optionList = ref<OptionListItem[]>([]);
  38. const search = async () => {
  39. optionList.value = [];
  40. if (!props.teachingId) return;
  41. const resData = await agentQuery(props.teachingId);
  42. optionList.value = (resData || []).map((item) => {
  43. return { ...item, value: item.id, label: item.name };
  44. });
  45. };
  46. const onChange = () => {
  47. const selectedData = props.multiple
  48. ? optionList.value.filter(
  49. (item) => selected.value && selected.value.includes(item.value)
  50. )
  51. : optionList.value.filter((item) => selected.value === item.value);
  52. emit('update:modelValue', selected.value);
  53. emit('change', props.multiple ? selectedData : selectedData[0]);
  54. };
  55. watch(
  56. () => props.modelValue,
  57. (val) => {
  58. selected.value = val;
  59. },
  60. {
  61. immediate: true,
  62. }
  63. );
  64. watch(
  65. () => props.teachingId,
  66. () => {
  67. emit('update:modelValue', '');
  68. emit('change', null);
  69. search();
  70. },
  71. {
  72. immediate: true,
  73. }
  74. );
  75. </script>