SourceDetailSelect.vue 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <template>
  2. <el-select
  3. v-model="selected"
  4. :clearable="clearable"
  5. :disabled="disabled"
  6. :placeholder="placeholder"
  7. value-key="id"
  8. @change="select"
  9. >
  10. <el-option
  11. v-for="item in optionList"
  12. :key="item.id"
  13. :label="item.name"
  14. :value="item"
  15. ></el-option>
  16. </el-select>
  17. </template>
  18. <script>
  19. import { sourceDetailPageListApi } from "../../modules/question/api";
  20. export default {
  21. name: "SourceDetailSelect",
  22. props: {
  23. value: {
  24. type: Object,
  25. default() {
  26. return {
  27. questionType: "",
  28. sourceDetailId: "",
  29. };
  30. },
  31. },
  32. courseId: {
  33. type: [String, Number],
  34. default: "",
  35. },
  36. disabled: { type: Boolean, default: false },
  37. placeholder: { type: String, default: "请选择题型" },
  38. clearable: { type: Boolean, default: true },
  39. },
  40. data() {
  41. return {
  42. selected: null,
  43. optionList: [],
  44. };
  45. },
  46. watch: {
  47. value: {
  48. immediate: true,
  49. handler(val) {
  50. this.selected = {
  51. questionType: val.questionType,
  52. id: val.sourceDetailId,
  53. };
  54. },
  55. },
  56. courseId(val, oldval) {
  57. if (val === oldval) return;
  58. this.search();
  59. },
  60. },
  61. mounted() {
  62. this.search();
  63. },
  64. methods: {
  65. async search() {
  66. if (!this.courseId) {
  67. this.optionList = [];
  68. this.selected = null;
  69. this.select();
  70. return;
  71. }
  72. const res = await sourceDetailPageListApi({
  73. pageNumber: 1,
  74. pageSize: 100,
  75. courseId: this.courseId,
  76. rootOrgId: this.$store.state.user.rootOrgId,
  77. });
  78. this.optionList = res.data.content || [];
  79. },
  80. select() {
  81. if (this.selected) {
  82. const selectVal = Object.assign({}, this.value, {
  83. questionType: this.selected.questionType,
  84. sourceDetailId: this.selected.id,
  85. sourceDetailName: this.selected.name,
  86. });
  87. this.$emit("input", selectVal);
  88. this.$emit("change", this.selected);
  89. } else {
  90. this.$emit("input", this.value);
  91. this.$emit("change", null);
  92. }
  93. },
  94. },
  95. };
  96. </script>