ModifyCourse.vue 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <template>
  2. <el-dialog
  3. class="modify-course"
  4. :visible.sync="modalIsShow"
  5. :title="title"
  6. top="10vh"
  7. width="448px"
  8. :close-on-click-modal="false"
  9. :close-on-press-escape="false"
  10. append-to-body
  11. @open="visibleChange"
  12. >
  13. <el-form
  14. ref="modalFormComp"
  15. :model="modalForm"
  16. :rules="rules"
  17. :key="modalForm.id"
  18. label-position="top"
  19. >
  20. <el-form-item prop="courseName" label="课程名称:">
  21. <el-input
  22. v-model.trim="modalForm.courseName"
  23. placeholder="请输入课程名称"
  24. clearable
  25. ></el-input>
  26. </el-form-item>
  27. <el-form-item prop="courseCode" label="课程编码:">
  28. <el-input
  29. v-model.trim="modalForm.courseCode"
  30. placeholder="请输入课程编码"
  31. clearable
  32. ></el-input>
  33. </el-form-item>
  34. <el-form-item prop="teachingRoomId" label="所属教研室:">
  35. <teaching-room-select
  36. v-model.trim="modalForm.teachingRoomId"
  37. style="width:100%;"
  38. clearable
  39. ></teaching-room-select>
  40. </el-form-item>
  41. <el-form-item prop="clazz" label="授课班级:">
  42. <el-input
  43. v-model.trim="modalForm.clazz"
  44. type="textarea"
  45. placeholder="请输入授课班级"
  46. clearable
  47. ></el-input>
  48. <p class="tips-info">多个班级请用英文逗号分隔</p>
  49. </el-form-item>
  50. </el-form>
  51. <div slot="footer">
  52. <el-button type="primary" :disabled="isSubmit" @click="submit"
  53. >确认</el-button
  54. >
  55. <el-button @click="cancel">取消</el-button>
  56. </div>
  57. </el-dialog>
  58. </template>
  59. <script>
  60. import { updateCourse, getCode } from "../api";
  61. const initModalForm = {
  62. id: null,
  63. courseName: "",
  64. courseCode: "",
  65. teachingRoomId: "",
  66. clazz: ""
  67. };
  68. export default {
  69. name: "modify-course",
  70. props: {
  71. instance: {
  72. type: Object,
  73. default() {
  74. return {};
  75. }
  76. }
  77. },
  78. computed: {
  79. isEdit() {
  80. return !!this.instance.id;
  81. },
  82. title() {
  83. return (this.isEdit ? "编辑" : "新增") + "课程";
  84. }
  85. },
  86. data() {
  87. return {
  88. modalIsShow: false,
  89. isSubmit: false,
  90. modalForm: { ...initModalForm },
  91. rules: {
  92. courseName: [
  93. {
  94. required: true,
  95. // pattern: /^[0-9a-zA-Z\u4E00-\u9FA5]{1,20}$/,
  96. // message: "课程名称只能输入汉字、数字和字母,长度不能超过20",
  97. message: "课程名称不能超过30个字",
  98. max: 30,
  99. trigger: "change"
  100. }
  101. ],
  102. courseCode: [
  103. {
  104. required: true,
  105. pattern: /^[0-9a-zA-Z_-]{3,30}$/,
  106. message: "课程编码只能由数字字母短横线组成,长度在3-30之间",
  107. trigger: "change"
  108. }
  109. ],
  110. teachingRoomId: [
  111. {
  112. required: true,
  113. message: "请选择所属教研室",
  114. trigger: "change"
  115. }
  116. ],
  117. clazz: [
  118. {
  119. required: true,
  120. message: "请输入授课班级",
  121. trigger: "change"
  122. }
  123. ]
  124. }
  125. };
  126. },
  127. methods: {
  128. initData(val) {
  129. if (val.id) {
  130. this.modalForm = this.$objAssign(initModalForm, val);
  131. } else {
  132. this.modalForm = { ...initModalForm };
  133. this.createCode();
  134. }
  135. },
  136. async createCode() {
  137. this.modalForm.courseCode = await getCode("COURSE_CODE");
  138. },
  139. visibleChange() {
  140. this.initData(this.instance);
  141. },
  142. cancel() {
  143. this.modalIsShow = false;
  144. },
  145. open() {
  146. this.modalIsShow = true;
  147. },
  148. async submit() {
  149. const valid = await this.$refs.modalFormComp.validate().catch(() => {});
  150. if (!valid) return;
  151. if (this.isSubmit) return;
  152. this.isSubmit = true;
  153. let datas = { ...this.modalForm };
  154. datas.clazzList = datas.clazz.split(",");
  155. const data = await updateCourse(datas).catch(() => {
  156. this.isSubmit = false;
  157. });
  158. if (!data) return;
  159. this.isSubmit = false;
  160. this.$message.success(this.title + "成功!");
  161. this.$emit("modified");
  162. this.cancel();
  163. }
  164. }
  165. };
  166. </script>