ModifyStudent.vue 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. <template>
  2. <el-dialog
  3. class="modify-student"
  4. :visible.sync="modalIsShow"
  5. :title="title"
  6. top="10px"
  7. width="448px"
  8. :close-on-click-modal="false"
  9. :close-on-press-escape="false"
  10. append-to-body
  11. destroy-on-close
  12. @open="visibleChange"
  13. >
  14. <el-form
  15. ref="modalFormComp"
  16. :model="modalForm"
  17. :rules="rules"
  18. :key="modalForm.id"
  19. label-position="top"
  20. >
  21. <el-form-item prop="studentName" label="姓名:">
  22. <el-input
  23. v-model.trim="modalForm.studentName"
  24. placeholder="请输入姓名"
  25. clearable
  26. ></el-input>
  27. </el-form-item>
  28. <el-form-item prop="studentCode" label="学号:">
  29. <el-input
  30. v-model.trim="modalForm.studentCode"
  31. placeholder="请输入学号"
  32. clearable
  33. ></el-input>
  34. </el-form-item>
  35. <el-form-item prop="phoneNumber" label="手机号:">
  36. <el-input
  37. v-model.trim="modalForm.phoneNumber"
  38. placeholder="请输入手机号"
  39. clearable
  40. ></el-input>
  41. </el-form-item>
  42. <el-form-item prop="campusId" label="所属校区:">
  43. <campus-select
  44. v-model="modalForm.campusId"
  45. placeholder="请选择所属校区"
  46. style="width: 100%;"
  47. @change="updateCourses"
  48. ></campus-select>
  49. </el-form-item>
  50. <el-form-item prop="collegeId" label="学院:">
  51. <college-select
  52. v-model="modalForm.collegeId"
  53. placeholder="请选择学院"
  54. style="width: 100%;"
  55. ></college-select>
  56. </el-form-item>
  57. <el-form-item prop="majorId" label="专业:">
  58. <major-select
  59. v-model="modalForm.majorId"
  60. :college-id="modalForm.collegeId"
  61. cascader
  62. placeholder="请选择专业"
  63. style="width: 100%;"
  64. @change="updateCourses"
  65. ></major-select>
  66. </el-form-item>
  67. <el-form-item prop="clazzId" label="班级:">
  68. <el-select
  69. v-model="modalForm.clazzId"
  70. placeholder="请选择班级"
  71. style="width: 100%;"
  72. >
  73. <el-option
  74. v-for="item in courses"
  75. :key="item.id"
  76. :value="item.id"
  77. :label="item.name"
  78. >
  79. </el-option>
  80. </el-select>
  81. </el-form-item>
  82. </el-form>
  83. <div slot="footer">
  84. <el-button type="primary" :disabled="isSubmit" @click="submit"
  85. >确认</el-button
  86. >
  87. <el-button @click="cancel">取消</el-button>
  88. </div>
  89. </el-dialog>
  90. </template>
  91. <script>
  92. import { updateStudent, unitQueryByType } from "../api";
  93. const initModalForm = {
  94. id: null,
  95. studentName: "",
  96. studentCode: "",
  97. phoneNumber: "",
  98. campusId: "",
  99. collegeId: "",
  100. majorId: "",
  101. clazzId: ""
  102. };
  103. export default {
  104. name: "modify-student",
  105. props: {
  106. instance: {
  107. type: Object,
  108. default() {
  109. return {};
  110. }
  111. }
  112. },
  113. computed: {
  114. isEdit() {
  115. return !!this.instance.id;
  116. },
  117. title() {
  118. return (this.isEdit ? "编辑" : "新增") + "学生";
  119. }
  120. },
  121. data() {
  122. return {
  123. modalIsShow: false,
  124. isSubmit: false,
  125. modalForm: { ...initModalForm },
  126. rules: {
  127. studentName: [
  128. {
  129. required: true,
  130. message: "请输入姓名",
  131. trigger: "change"
  132. },
  133. {
  134. message: "姓名不能超过50个字",
  135. max: 50,
  136. trigger: "change"
  137. }
  138. ],
  139. studentCode: [
  140. {
  141. required: true,
  142. message: "请输入学号",
  143. trigger: "change"
  144. },
  145. {
  146. message: "学号不能超过50个字",
  147. max: 50,
  148. trigger: "change"
  149. }
  150. ],
  151. phoneNumber: [
  152. {
  153. required: false,
  154. pattern: /^1\d{10}$/,
  155. message: "请输入合适的手机号码",
  156. trigger: "change"
  157. }
  158. ],
  159. campusId: [
  160. {
  161. required: true,
  162. message: "请选择校区",
  163. trigger: "change"
  164. }
  165. ],
  166. collegeId: [
  167. {
  168. required: true,
  169. message: "请选择学院",
  170. trigger: "change"
  171. }
  172. ],
  173. majorId: [
  174. {
  175. required: true,
  176. message: "请选择专业",
  177. trigger: "change"
  178. }
  179. ],
  180. clazzId: [
  181. {
  182. required: true,
  183. message: "请选择班级",
  184. trigger: "change"
  185. }
  186. ]
  187. },
  188. courses: []
  189. };
  190. },
  191. methods: {
  192. initData(val) {
  193. if (val.id) {
  194. this.modalForm = this.$objAssign(initModalForm, val);
  195. this.getCourses();
  196. } else {
  197. this.modalForm = { ...initModalForm };
  198. }
  199. },
  200. visibleChange() {
  201. this.initData(this.instance);
  202. },
  203. cancel() {
  204. this.modalIsShow = false;
  205. },
  206. open() {
  207. this.modalIsShow = true;
  208. },
  209. updateCourses() {
  210. this.modalForm.clazzId = "";
  211. this.getCourses();
  212. },
  213. async getCourses() {
  214. this.courses = [];
  215. if (!this.modalForm.campusId || !this.modalForm.majorId) return;
  216. const res = await unitQueryByType(
  217. {
  218. campusId: this.modalForm.campusId,
  219. majorId: this.modalForm.majorId
  220. },
  221. "CLAZZ"
  222. );
  223. this.courses = res;
  224. },
  225. async submit() {
  226. const valid = await this.$refs.modalFormComp.validate().catch(() => {});
  227. if (!valid) return;
  228. if (this.isSubmit) return;
  229. this.isSubmit = true;
  230. const data = await updateStudent(this.modalForm).catch(() => {
  231. this.isSubmit = false;
  232. });
  233. if (!data) return;
  234. this.isSubmit = false;
  235. this.$message.success(this.title + "成功!");
  236. this.$emit("modified");
  237. this.cancel();
  238. }
  239. }
  240. };
  241. </script>