SourceDetailSelect.vue 2.2 KB

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