ModifyStudent.vue 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. pattern: /^[a-zA-Z0-9]{1,50}$/,
  139. message: "学号只能由数字、字母组成",
  140. trigger: "change",
  141. },
  142. {
  143. message: "学号不能超过50个字",
  144. max: 50,
  145. trigger: "change",
  146. },
  147. ],
  148. phoneNumber: [
  149. {
  150. required: false,
  151. pattern: /^1\d{10}$/,
  152. message: "请输入合适的手机号码",
  153. trigger: "change",
  154. },
  155. ],
  156. collegeId: [
  157. {
  158. required: false,
  159. message: "请选择机构",
  160. trigger: "change",
  161. },
  162. ],
  163. majorId: [
  164. {
  165. required: false,
  166. message: "请选择专业",
  167. trigger: "change",
  168. },
  169. ],
  170. clazzId: [
  171. {
  172. required: false,
  173. message: "请选择班级",
  174. trigger: "change",
  175. },
  176. ],
  177. },
  178. clazzList: [],
  179. };
  180. },
  181. methods: {
  182. initData(val) {
  183. if (val.id) {
  184. this.modalForm = this.$objAssign(initModalForm, val);
  185. this.getClazz();
  186. } else {
  187. this.modalForm = { ...initModalForm };
  188. }
  189. },
  190. visibleChange() {
  191. this.initData(this.instance);
  192. },
  193. cancel() {
  194. this.modalIsShow = false;
  195. },
  196. open() {
  197. this.modalIsShow = true;
  198. },
  199. updateClazz() {
  200. this.modalForm.clazzId = "";
  201. this.getClazz();
  202. },
  203. async getClazz() {
  204. this.clazzList = [];
  205. if (!this.modalForm.majorId) return;
  206. const res = await unitQueryByType(
  207. {
  208. majorId: this.modalForm.majorId,
  209. },
  210. "CLAZZ"
  211. );
  212. this.clazzList = res;
  213. },
  214. async submit() {
  215. const valid = await this.$refs.modalFormComp.validate().catch(() => {});
  216. if (!valid) return;
  217. if (this.isSubmit) return;
  218. this.isSubmit = true;
  219. const data = await updateStudent(this.modalForm).catch(() => {
  220. this.isSubmit = false;
  221. });
  222. if (!data) return;
  223. this.isSubmit = false;
  224. this.$message.success(this.title + "成功!");
  225. this.$emit("modified");
  226. this.cancel();
  227. },
  228. },
  229. };
  230. </script>