ModifyStudent.vue 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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="collegeId" label="学院:">
  43. <college-select
  44. v-model="modalForm.collegeId"
  45. placeholder="学院"
  46. style="width: 100%;"
  47. ></college-select>
  48. </el-form-item>
  49. <el-form-item prop="majorId" label="专业:">
  50. <major-select
  51. v-model="modalForm.majorId"
  52. :college-id="modalForm.collegeId"
  53. cascader
  54. placeholder="请选择专业"
  55. style="width: 100%;"
  56. @change="updateClazz"
  57. ></major-select>
  58. </el-form-item>
  59. <el-form-item prop="clazzId" label="班级:">
  60. <el-select
  61. v-model="modalForm.clazzId"
  62. placeholder="请选择班级"
  63. style="width: 100%;"
  64. filterable
  65. >
  66. <el-option
  67. v-for="item in clazzList"
  68. :key="item.id"
  69. :value="item.id"
  70. :label="item.name"
  71. >
  72. </el-option>
  73. </el-select>
  74. </el-form-item>
  75. </el-form>
  76. <div slot="footer">
  77. <el-button type="primary" :disabled="isSubmit" @click="submit"
  78. >确认</el-button
  79. >
  80. <el-button @click="cancel">取消</el-button>
  81. </div>
  82. </el-dialog>
  83. </template>
  84. <script>
  85. import { updateStudent, unitQueryByType } from "../api";
  86. const initModalForm = {
  87. id: null,
  88. studentName: "",
  89. studentCode: "",
  90. phoneNumber: "",
  91. collegeId: "",
  92. majorId: "",
  93. clazzId: ""
  94. };
  95. export default {
  96. name: "modify-student",
  97. props: {
  98. instance: {
  99. type: Object,
  100. default() {
  101. return {};
  102. }
  103. }
  104. },
  105. computed: {
  106. isEdit() {
  107. return !!this.instance.id;
  108. },
  109. title() {
  110. return (this.isEdit ? "编辑" : "新增") + "学生";
  111. }
  112. },
  113. data() {
  114. return {
  115. modalIsShow: false,
  116. isSubmit: false,
  117. modalForm: { ...initModalForm },
  118. rules: {
  119. studentName: [
  120. {
  121. required: true,
  122. message: "请输入姓名",
  123. trigger: "change"
  124. },
  125. {
  126. message: "姓名不能超过50个字",
  127. max: 50,
  128. trigger: "change"
  129. }
  130. ],
  131. studentCode: [
  132. {
  133. required: true,
  134. message: "请输入学号",
  135. trigger: "change"
  136. },
  137. {
  138. message: "学号不能超过50个字",
  139. max: 50,
  140. trigger: "change"
  141. }
  142. ],
  143. phoneNumber: [
  144. {
  145. required: false,
  146. pattern: /^1\d{10}$/,
  147. message: "请输入合适的手机号码",
  148. trigger: "change"
  149. }
  150. ],
  151. collegeId: [
  152. {
  153. required: false,
  154. message: "请选择机构",
  155. trigger: "change"
  156. }
  157. ],
  158. majorId: [
  159. {
  160. required: false,
  161. message: "请选择专业",
  162. trigger: "change"
  163. }
  164. ],
  165. clazzId: [
  166. {
  167. required: false,
  168. message: "请选择班级",
  169. trigger: "change"
  170. }
  171. ]
  172. },
  173. clazzList: []
  174. };
  175. },
  176. methods: {
  177. initData(val) {
  178. if (val.id) {
  179. this.modalForm = this.$objAssign(initModalForm, val);
  180. this.getClazz();
  181. } else {
  182. this.modalForm = { ...initModalForm };
  183. }
  184. },
  185. visibleChange() {
  186. this.initData(this.instance);
  187. },
  188. cancel() {
  189. this.modalIsShow = false;
  190. },
  191. open() {
  192. this.modalIsShow = true;
  193. },
  194. updateClazz() {
  195. this.modalForm.clazzId = "";
  196. this.getClazz();
  197. },
  198. async getClazz() {
  199. this.clazzList = [];
  200. if (!this.modalForm.majorId) return;
  201. const res = await unitQueryByType(
  202. {
  203. majorId: this.modalForm.majorId
  204. },
  205. "CLAZZ"
  206. );
  207. this.clazzList = res;
  208. },
  209. async submit() {
  210. const valid = await this.$refs.modalFormComp.validate().catch(() => {});
  211. if (!valid) return;
  212. if (this.isSubmit) return;
  213. this.isSubmit = true;
  214. const data = await updateStudent(this.modalForm).catch(() => {
  215. this.isSubmit = false;
  216. });
  217. if (!data) return;
  218. this.isSubmit = false;
  219. this.$message.success(this.title + "成功!");
  220. this.$emit("modified");
  221. this.cancel();
  222. }
  223. }
  224. };
  225. </script>