ModifyStudent.vue 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. <template>
  2. <Modal
  3. class="modify-student"
  4. v-model="modalIsShow"
  5. :title="title"
  6. :mask-closable="false"
  7. width="450"
  8. @on-visible-change="visibleChange"
  9. >
  10. <Form
  11. ref="modalFormComp"
  12. class="modal-form"
  13. :model="modalForm"
  14. :rules="rules"
  15. :key="modalForm.id"
  16. :label-width="100"
  17. style="width: 340px"
  18. >
  19. <FormItem prop="name" label="姓名">
  20. <Input
  21. size="large"
  22. v-model.trim="modalForm.name"
  23. placeholder="请输入姓名"
  24. clearable
  25. ></Input>
  26. </FormItem>
  27. <FormItem prop="examNumber" label="考号">
  28. <Input
  29. size="large"
  30. v-model.trim="modalForm.examNumber"
  31. placeholder="请输入考号"
  32. clearable
  33. ></Input>
  34. </FormItem>
  35. <FormItem prop="areaCode" label="考区">
  36. <Select
  37. size="large"
  38. v-model="modalForm.areaCode"
  39. @on-change="areaChange"
  40. placeholder="请选择考区"
  41. >
  42. <Option
  43. v-for="area in cascadeList"
  44. :key="area.areaCode"
  45. :value="area.areaCode"
  46. :label="area.areaName"
  47. ></Option>
  48. </Select>
  49. </FormItem>
  50. <FormItem prop="school" label="学校">
  51. <Select
  52. size="large"
  53. v-model="modalForm.school"
  54. @on-change="schoolChange"
  55. placeholder="请选择学校"
  56. filterable
  57. allow-create
  58. @on-create="createSchool"
  59. >
  60. <Option
  61. v-for="(item, index) in schools"
  62. :key="index"
  63. :value="item.school"
  64. :label="item.school"
  65. ></Option>
  66. </Select>
  67. </FormItem>
  68. <FormItem prop="examRoom" label="考场">
  69. <Select
  70. size="large"
  71. v-model="modalForm.examRoom"
  72. placeholder="请选择考场"
  73. filterable
  74. allow-create
  75. @on-create="createRoom"
  76. :key="modalForm.school"
  77. >
  78. <Option
  79. v-for="(room, index) in rooms"
  80. :key="index"
  81. :value="room"
  82. :label="room"
  83. ></Option>
  84. </Select>
  85. </FormItem>
  86. </Form>
  87. <div slot="footer">
  88. <Button shape="circle" type="primary" :disabled="isSubmit" @click="submit"
  89. >确认</Button
  90. >
  91. <Button shape="circle" @click="cancel">取消</Button>
  92. </div>
  93. </Modal>
  94. </template>
  95. <script>
  96. import { updateStudent } from "@/api";
  97. const initModalForm = {
  98. id: null,
  99. name: "",
  100. workId: null,
  101. examNumber: "",
  102. areaName: "",
  103. areaCode: "",
  104. school: "",
  105. examRoom: "",
  106. };
  107. export default {
  108. name: "modify-student",
  109. props: {
  110. instance: {
  111. type: Object,
  112. default() {
  113. return {};
  114. },
  115. },
  116. cascadeList: {
  117. type: Array,
  118. default() {
  119. return [];
  120. },
  121. },
  122. },
  123. computed: {
  124. isEdit() {
  125. return !!this.instance.id;
  126. },
  127. title() {
  128. return (this.isEdit ? "编辑" : "新增") + "考生信息";
  129. },
  130. },
  131. data() {
  132. return {
  133. modalIsShow: false,
  134. isSubmit: false,
  135. schools: [],
  136. rooms: [],
  137. modalForm: { ...initModalForm },
  138. rules: {
  139. name: [
  140. {
  141. required: true,
  142. message: "请输入姓名",
  143. trigger: "change",
  144. },
  145. {
  146. min: 2,
  147. max: 20,
  148. message: "姓名长度只能介于2到20之间",
  149. },
  150. ],
  151. examNumber: [
  152. {
  153. required: true,
  154. message: "请输入考号",
  155. trigger: "change",
  156. },
  157. {
  158. min: 2,
  159. max: 20,
  160. message: "考号长度只能介于2到20之间",
  161. },
  162. ],
  163. areaCode: [
  164. {
  165. required: true,
  166. message: "请选择考区",
  167. trigger: "change",
  168. },
  169. ],
  170. school: [
  171. {
  172. required: true,
  173. message: "请选择学校",
  174. trigger: "change",
  175. },
  176. ],
  177. examRoom: [
  178. {
  179. required: true,
  180. message: "请选择考场",
  181. trigger: "change",
  182. },
  183. ],
  184. },
  185. };
  186. },
  187. methods: {
  188. initData(val) {
  189. this.$refs.modalFormComp.resetFields();
  190. this.modalForm = this.$objAssign(initModalForm, val);
  191. if (val.id) {
  192. this.reviewInfo();
  193. }
  194. },
  195. visibleChange(visible) {
  196. if (visible) {
  197. this.initData(this.instance);
  198. } else {
  199. this.$emit("on-close");
  200. }
  201. },
  202. reviewInfo() {
  203. const curArea = this.cascadeList.find(
  204. (item) => item.areaCode === this.modalForm.areaCode
  205. );
  206. this.schools = curArea.schools;
  207. const curSchool = this.schools.find(
  208. (item) => item.school === this.modalForm.school
  209. );
  210. this.rooms = curSchool.rooms;
  211. },
  212. areaChange() {
  213. const curArea = this.cascadeList.find(
  214. (item) => item.areaCode === this.modalForm.areaCode
  215. );
  216. this.schools = curArea ? curArea.schools : [];
  217. this.modalForm.areaName = curArea.areaName;
  218. this.rooms = [];
  219. this.modalForm.examRoom = null;
  220. if (curArea.schools.length === 1) {
  221. this.modalForm.school = curArea.schools[0].school;
  222. this.schoolChange();
  223. }
  224. },
  225. schoolChange() {
  226. const curSchool = this.schools.find(
  227. (item) => item.school === this.modalForm.school
  228. );
  229. this.rooms = curSchool ? curSchool.rooms : [];
  230. this.modalForm.examRoom = null;
  231. // 后触发更改
  232. // console.log("school change");
  233. },
  234. createSchool(val) {
  235. this.schools.push({ school: val, rooms: [] });
  236. // 先创建
  237. // console.log("school create");
  238. },
  239. createRoom(val) {
  240. this.rooms.push(val);
  241. },
  242. cancel() {
  243. this.modalIsShow = false;
  244. },
  245. open() {
  246. this.modalIsShow = true;
  247. },
  248. async submit() {
  249. const valid = await this.$refs.modalFormComp.validate();
  250. if (!valid) return;
  251. if (this.isSubmit) return;
  252. this.isSubmit = true;
  253. const data = await updateStudent(this.modalForm).catch(() => {});
  254. this.isSubmit = false;
  255. if (!data) return;
  256. this.$Message.success(this.title + "成功!");
  257. this.$emit("modified");
  258. this.cancel();
  259. },
  260. },
  261. };
  262. </script>