ModifyStudent.vue 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <template>
  2. <el-dialog
  3. class="modify-student"
  4. :visible.sync="modalIsShow"
  5. :title="title"
  6. top="10vh"
  7. width="500px"
  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-width="100px"
  19. >
  20. <el-form-item prop="studentName" label="姓名:">
  21. <el-input
  22. v-model.trim="modalForm.studentName"
  23. placeholder="请输入姓名"
  24. clearable
  25. ></el-input>
  26. </el-form-item>
  27. <el-form-item prop="studentCode" label="学号:">
  28. <el-input
  29. v-model.trim="modalForm.studentCode"
  30. placeholder="请输入学号"
  31. clearable
  32. ></el-input>
  33. </el-form-item>
  34. <el-form-item prop="phoneNumber" label="手机号:">
  35. <el-input
  36. v-model.trim="modalForm.phoneNumber"
  37. placeholder="请输入手机号"
  38. clearable
  39. ></el-input>
  40. </el-form-item>
  41. <el-form-item prop="campusId" label="校区:">
  42. <campus-select
  43. v-model.trim="modalForm.campusId"
  44. clearable
  45. ></campus-select>
  46. </el-form-item>
  47. <el-form-item prop="clazz" label="班级信息:">
  48. <el-input
  49. v-model.trim="modalForm.clazz"
  50. placeholder="请输入班级信息"
  51. clearable
  52. ></el-input>
  53. </el-form-item>
  54. </el-form>
  55. <div slot="footer">
  56. <el-button type="primary" :disabled="isSubmit" @click="submit"
  57. >确认</el-button
  58. >
  59. <el-button type="danger" @click="cancel" plain>取消</el-button>
  60. </div>
  61. </el-dialog>
  62. </template>
  63. <script>
  64. import { updateStudent } from "../api";
  65. import { phone } from "@/plugins/formRules";
  66. const initModalForm = {
  67. id: null,
  68. studentName: "",
  69. studentCode: "",
  70. phoneNumber: "",
  71. campusId: "",
  72. clazz: ""
  73. };
  74. export default {
  75. name: "modify-student",
  76. props: {
  77. instance: {
  78. type: Object,
  79. default() {
  80. return {};
  81. }
  82. }
  83. },
  84. computed: {
  85. isEdit() {
  86. return !!this.instance.id;
  87. },
  88. title() {
  89. return (this.isEdit ? "编辑" : "新增") + "学生";
  90. }
  91. },
  92. data() {
  93. return {
  94. modalIsShow: false,
  95. isSubmit: false,
  96. modalForm: { ...initModalForm },
  97. rules: {
  98. studentName: [
  99. {
  100. required: true,
  101. message: "请输入姓名",
  102. trigger: "change"
  103. },
  104. {
  105. message: "姓名不能超过50个字",
  106. max: 50,
  107. trigger: "change"
  108. }
  109. ],
  110. studentCode: [
  111. {
  112. required: true,
  113. message: "请输入学号",
  114. trigger: "change"
  115. },
  116. {
  117. message: "学号不能超过50个字",
  118. max: 50,
  119. trigger: "change"
  120. }
  121. ],
  122. phoneNumber: phone,
  123. campusId: [
  124. {
  125. required: true,
  126. message: "请选择校区",
  127. trigger: "change"
  128. }
  129. ],
  130. clazz: [
  131. {
  132. required: true,
  133. message: "请输入班级信息",
  134. trigger: "change"
  135. }
  136. ]
  137. }
  138. };
  139. },
  140. methods: {
  141. initData(val) {
  142. if (val.id) {
  143. this.modalForm = this.$objAssign(initModalForm, val);
  144. } else {
  145. this.modalForm = { ...initModalForm };
  146. }
  147. },
  148. visibleChange() {
  149. this.initData(this.instance);
  150. },
  151. cancel() {
  152. this.modalIsShow = false;
  153. },
  154. open() {
  155. this.modalIsShow = true;
  156. },
  157. async submit() {
  158. const valid = await this.$refs.modalFormComp.validate().catch(() => {});
  159. if (!valid) return;
  160. if (this.isSubmit) return;
  161. this.isSubmit = true;
  162. const data = await updateStudent(this.modalForm).catch(() => {
  163. this.isSubmit = false;
  164. });
  165. if (!data) return;
  166. this.isSubmit = false;
  167. this.$message.success(this.title + "成功!");
  168. this.$emit("modified");
  169. this.cancel();
  170. }
  171. }
  172. };
  173. </script>