ModifyCourse.vue 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <template>
  2. <el-dialog
  3. class="modify-data"
  4. :visible.sync="modalIsShow"
  5. :title="title"
  6. top="10vh"
  7. :close-on-click-modal="false"
  8. :close-on-press-escape="false"
  9. append-to-body
  10. @open="visibleChange"
  11. >
  12. <el-form
  13. ref="modalFormComp"
  14. :model="modalForm"
  15. :rules="rules"
  16. :key="modalForm.id"
  17. label-width="100px"
  18. >
  19. <el-form-item prop="courseName" label="科目名称:">
  20. <el-input
  21. v-model.trim="modalForm.courseName"
  22. placeholder="请输入科目名称"
  23. clearable
  24. ></el-input>
  25. </el-form-item>
  26. <el-form-item prop="courseCode" label="科目代码:">
  27. <el-input
  28. v-model.trim="modalForm.courseCode"
  29. placeholder="请输入科目代码"
  30. clearable
  31. ></el-input>
  32. </el-form-item>
  33. </el-form>
  34. <div slot="footer">
  35. <el-button type="danger" @click="cancel" plain>取消</el-button>
  36. <el-button type="primary" :disabled="isSubmit" @click="submit"
  37. >确认</el-button
  38. >
  39. </div>
  40. </el-dialog>
  41. </template>
  42. <script>
  43. import { updateCourse } from "../api";
  44. import { commonCode } from "@/plugins/formRules";
  45. const initModalForm = {
  46. courseId: "",
  47. courseName: "",
  48. courseCode: ""
  49. };
  50. export default {
  51. name: "modify-data",
  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. courseName: [
  75. {
  76. required: true,
  77. message: "请输入科目名称",
  78. trigger: "change"
  79. }
  80. ],
  81. courseCode: commonCode({ prop: "科目代码" })
  82. }
  83. };
  84. },
  85. methods: {
  86. initData(val) {
  87. if (val.id) {
  88. this.modalForm = this.$objAssign(initModalForm, val);
  89. this.modalForm.courseId = val["id"] || "";
  90. } else {
  91. this.modalForm = { ...initModalForm };
  92. }
  93. },
  94. visibleChange() {
  95. this.initData(this.instance);
  96. },
  97. cancel() {
  98. this.modalIsShow = false;
  99. },
  100. open() {
  101. this.modalIsShow = true;
  102. },
  103. async submit() {
  104. const valid = await this.$refs.modalFormComp.validate().catch(() => {});
  105. if (!valid) return;
  106. if (this.isSubmit) return;
  107. this.isSubmit = true;
  108. const data = await updateCourse(this.modalForm).catch(() => {
  109. this.isSubmit = false;
  110. });
  111. if (!data) return;
  112. this.isSubmit = false;
  113. this.$message.success(this.title + "成功!");
  114. this.$emit("modified");
  115. this.cancel();
  116. }
  117. }
  118. };
  119. </script>