ModifyCard.vue 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <template>
  2. <el-dialog
  3. custom-class="side-dialog"
  4. :visible.sync="modalIsShow"
  5. title="编辑"
  6. width="550px"
  7. :modal="true"
  8. append-to-body
  9. @open="visibleChange"
  10. >
  11. <el-form
  12. ref="modalFormComp"
  13. :key="modalForm.id"
  14. :model="modalForm"
  15. :rules="rules"
  16. label-width="100px"
  17. >
  18. <el-form-item label="所属课程:">
  19. <el-input v-model.trim="modalForm.course" readonly></el-input>
  20. </el-form-item>
  21. <el-form-item label="创建人:">
  22. <el-input v-model.trim="modalForm.creator" readonly></el-input>
  23. </el-form-item>
  24. <el-form-item prop="teacherId" label="命题老师:">
  25. <el-select
  26. v-model="modalForm.teacherId"
  27. class="width-full"
  28. placeholder="请选择"
  29. clearable
  30. filterable
  31. >
  32. <el-option
  33. v-for="item in teachers"
  34. :key="item.id"
  35. :value="item.id"
  36. :label="item.name"
  37. >
  38. </el-option>
  39. </el-select>
  40. </el-form-item>
  41. </el-form>
  42. <div slot="footer">
  43. <el-button type="primary" :disabled="isSubmit" @click="submit"
  44. >确认</el-button
  45. >
  46. <el-button @click="cancel">取消</el-button>
  47. </div>
  48. </el-dialog>
  49. </template>
  50. <script>
  51. import { cardUpdateApi, questionTeacherQueryApi } from "../api";
  52. const initModalForm = {
  53. id: null,
  54. course: "",
  55. creator: "",
  56. teacherId: "",
  57. };
  58. export default {
  59. name: "ModifyCard",
  60. props: {
  61. instance: {
  62. type: Object,
  63. default() {
  64. return {};
  65. },
  66. },
  67. },
  68. data() {
  69. return {
  70. modalIsShow: false,
  71. isSubmit: false,
  72. modalForm: { ...initModalForm },
  73. teachers: [],
  74. rules: {
  75. teacherId: [
  76. {
  77. required: true,
  78. message: "请选择命题老师",
  79. trigger: "change",
  80. },
  81. ],
  82. },
  83. };
  84. },
  85. mounted() {
  86. this.getTeachers("");
  87. },
  88. methods: {
  89. async getTeachers(name) {
  90. const res = await questionTeacherQueryApi(name);
  91. this.teachers = res.data.content;
  92. },
  93. visibleChange() {
  94. this.modalForm = this.$objAssign(initModalForm, this.instance);
  95. const hasTeacher = this.teachers.find(
  96. (item) => item.id === this.instance.teacherId
  97. );
  98. if (!hasTeacher) {
  99. this.modalForm.teacherId = null;
  100. this.$nextTick(() => {
  101. this.$refs.modalFormComp.validateField("teacherId");
  102. });
  103. }
  104. this.isSubmit = false;
  105. },
  106. cancel() {
  107. this.modalIsShow = false;
  108. },
  109. open() {
  110. this.modalIsShow = true;
  111. },
  112. async submit() {
  113. const valid = await this.$refs.modalFormComp.validate().catch(() => {});
  114. if (!valid) return;
  115. if (this.isSubmit) return;
  116. this.isSubmit = true;
  117. let datas = {
  118. cardId: this.modalForm.id,
  119. teacherId: this.modalForm.teacherId,
  120. };
  121. const data = await cardUpdateApi(datas).catch(() => {
  122. this.isSubmit = false;
  123. });
  124. if (!data) return;
  125. this.isSubmit = false;
  126. this.$message.success("编辑成功!");
  127. this.$emit("modified");
  128. this.cancel();
  129. },
  130. },
  131. };
  132. </script>