ModifyExam.vue 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <template>
  2. <el-dialog
  3. class="modify-exam"
  4. :visible.sync="modalIsShow"
  5. :title="title"
  6. top="10vh"
  7. width="460px"
  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. label-width="100px"
  18. >
  19. <el-form-item prop="name" label="考试名称:">
  20. <el-input
  21. v-model.trim="modalForm.name"
  22. placeholder="请输入考试名称"
  23. clearable
  24. ></el-input>
  25. </el-form-item>
  26. <el-form-item prop="semesterId" label="所属学期:">
  27. <semester-select
  28. v-model="modalForm.semesterId"
  29. placeholder="请选择所属学期"
  30. :clearable="false"
  31. ></semester-select>
  32. </el-form-item>
  33. </el-form>
  34. <div slot="footer">
  35. <el-button type="primary" :disabled="isSubmit" @click="submit"
  36. >确认</el-button
  37. >
  38. <el-button @click="cancel">取消</el-button>
  39. </div>
  40. </el-dialog>
  41. </template>
  42. <script>
  43. import { updateExam } from "../api";
  44. import { slashesLimitValidator } from "@/plugins/formRules";
  45. const initModalForm = {
  46. id: null,
  47. semesterId: "",
  48. name: "",
  49. };
  50. export default {
  51. name: "modify-exam",
  52. props: {
  53. instance: {
  54. type: Object,
  55. default() {
  56. return {};
  57. },
  58. },
  59. },
  60. computed: {
  61. isEdit() {
  62. return !!this.instance.id;
  63. },
  64. title() {
  65. return (this.isEdit ? "编辑" : "新增") + "考试";
  66. },
  67. },
  68. data() {
  69. return {
  70. modalIsShow: false,
  71. isSubmit: false,
  72. modalForm: { ...initModalForm },
  73. rules: {
  74. name: [
  75. {
  76. required: true,
  77. message: "请输入考试名称",
  78. trigger: "change",
  79. },
  80. {
  81. message: "考试名称不能超过100字符",
  82. max: 100,
  83. trigger: "change",
  84. },
  85. ...slashesLimitValidator,
  86. ],
  87. semesterId: [
  88. {
  89. required: true,
  90. message: "请选择所属学期",
  91. trigger: "change",
  92. },
  93. ],
  94. },
  95. };
  96. },
  97. methods: {
  98. initData(val) {
  99. if (val.id) {
  100. this.modalForm = this.$objAssign(initModalForm, val);
  101. } else {
  102. this.modalForm = { ...initModalForm };
  103. }
  104. },
  105. visibleChange() {
  106. this.initData(this.instance);
  107. },
  108. cancel() {
  109. this.modalIsShow = false;
  110. },
  111. open() {
  112. this.modalIsShow = true;
  113. },
  114. async submit() {
  115. const valid = await this.$refs.modalFormComp.validate().catch(() => {});
  116. if (!valid) return;
  117. if (this.isSubmit) return;
  118. this.isSubmit = true;
  119. let datas = { ...this.modalForm };
  120. const data = await updateExam(datas).catch(() => {});
  121. this.isSubmit = false;
  122. if (!data) return;
  123. this.$message.success(this.title + "成功!");
  124. this.$emit("modified");
  125. this.cancel();
  126. },
  127. },
  128. };
  129. </script>