index.vue 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <template>
  2. <a-select
  3. v-model:value="selected"
  4. :placeholder="placeholder"
  5. :options="optionList"
  6. filter-option
  7. :multiple="false"
  8. :field-names="fieldNames"
  9. style="width: 200px"
  10. v-bind="attrs"
  11. @change="onChange"
  12. >
  13. </a-select>
  14. </template>
  15. <script setup lang="ts">
  16. import { ref, useAttrs, watch } from "vue";
  17. import { getSubjectList } from "@/ap/base";
  18. import { SubjectItem } from "@/ap/types/base";
  19. defineOptions({
  20. name: "SelectCourse",
  21. });
  22. const props = withDefaults(
  23. defineProps<{
  24. examId: number;
  25. value: string;
  26. placeholder?: string;
  27. }>(),
  28. {
  29. placeholder: "请选择",
  30. }
  31. );
  32. const emit = defineEmits(["update:value", "change"]);
  33. const attrs = useAttrs();
  34. const fieldNames = { label: "name", value: "code" };
  35. const selected = ref("");
  36. const optionList = ref<SubjectItem[]>([]);
  37. const search = async () => {
  38. optionList.value = [];
  39. const resData = await getSubjectList({ examId: props.examId });
  40. optionList.value = resData || [];
  41. };
  42. search();
  43. const onChange = () => {
  44. const selectedData = optionList.value.filter(
  45. (item) => selected.value === item.subjectCode
  46. );
  47. emit("update:value", selected.value);
  48. emit("change", selectedData[0]);
  49. };
  50. watch(
  51. () => props.value,
  52. (val) => {
  53. selected.value = val;
  54. },
  55. {
  56. immediate: true,
  57. }
  58. );
  59. </script>