CourseSelect.vue 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <template>
  2. <el-select
  3. v-model="selected"
  4. class="size-select"
  5. placeholder="请选择"
  6. @change="select"
  7. :style="styles"
  8. filterable
  9. clearable
  10. >
  11. <el-option
  12. v-for="item in optionList"
  13. :key="item.courseCode"
  14. :label="item.courseName"
  15. :value="item.courseCode"
  16. >
  17. <span>{{ item.courseName }}</span>
  18. </el-option>
  19. </el-select>
  20. </template>
  21. <script>
  22. import { searchCourses } from "@/api/examwork-course";
  23. export default {
  24. name: "CourseSelect",
  25. props: {
  26. value: String,
  27. examId: String,
  28. styles: { type: String },
  29. examRequired: {
  30. type: Boolean,
  31. default: false,
  32. },
  33. },
  34. data() {
  35. return {
  36. optionList: [],
  37. selected: "",
  38. };
  39. },
  40. async created() {
  41. this.search();
  42. },
  43. watch: {
  44. value: {
  45. immediate: true,
  46. handler(val) {
  47. this.selected = val;
  48. },
  49. },
  50. examId(val, oldval) {
  51. if (val !== oldval) {
  52. this.search("");
  53. this.$emit("input", "");
  54. this.$emit("change", {});
  55. }
  56. },
  57. },
  58. methods: {
  59. async search(query) {
  60. this.optionList = [];
  61. if (this.examRequired && !this.examId) return;
  62. const res = await searchCourses({
  63. examId: this.examId,
  64. courseName: query,
  65. pageNumber: 1,
  66. pageSize: 1000,
  67. });
  68. this.optionList = res?.data.data.records;
  69. },
  70. select() {
  71. this.$emit("input", this.selected);
  72. this.$emit("change", this.selected);
  73. },
  74. },
  75. };
  76. </script>
  77. <style></style>