ModifyCourse.vue 3.7 KB

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