ModifyStudent.vue 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. >
  57. <Option
  58. v-for="(item, index) in schools"
  59. :key="index"
  60. :value="item.school"
  61. :label="item.school"
  62. ></Option>
  63. </Select>
  64. </FormItem>
  65. <FormItem prop="examRoom" label="考场">
  66. <Select
  67. size="large"
  68. v-model="modalForm.examRoom"
  69. placeholder="请选择考场"
  70. >
  71. <Option
  72. v-for="(room, index) in rooms"
  73. :key="index"
  74. :value="room"
  75. :label="room"
  76. ></Option>
  77. </Select>
  78. </FormItem>
  79. </Form>
  80. <div slot="footer">
  81. <Button shape="circle" type="primary" :disabled="isSubmit" @click="submit"
  82. >确认</Button
  83. >
  84. <Button shape="circle" @click="cancel">取消</Button>
  85. </div>
  86. </Modal>
  87. </template>
  88. <script>
  89. import { updateStudent } from "@/api";
  90. const initModalForm = {
  91. id: null,
  92. name: "",
  93. examNumber: "",
  94. areaCode: "",
  95. school: "",
  96. examRoom: ""
  97. };
  98. export default {
  99. name: "modify-student",
  100. props: {
  101. instance: {
  102. type: Object,
  103. default() {
  104. return {};
  105. }
  106. },
  107. cascadeList: {
  108. type: Array,
  109. default() {
  110. return [];
  111. }
  112. }
  113. },
  114. computed: {
  115. isEdit() {
  116. return !!this.instance.id;
  117. },
  118. title() {
  119. return (this.isEdit ? "编辑" : "新增") + "考生信息";
  120. }
  121. },
  122. data() {
  123. return {
  124. modalIsShow: false,
  125. isSubmit: false,
  126. schools: [],
  127. rooms: [],
  128. modalForm: { ...initModalForm },
  129. rules: {
  130. name: [
  131. {
  132. required: true,
  133. message: "请输入姓名",
  134. trigger: "change"
  135. }
  136. ],
  137. examNumber: [
  138. {
  139. required: true,
  140. message: "请输入考号",
  141. trigger: "change"
  142. }
  143. ],
  144. areaCode: [
  145. {
  146. required: true,
  147. message: "请选择考区",
  148. trigger: "change"
  149. }
  150. ],
  151. schoolId: [
  152. {
  153. required: true,
  154. message: "请选择学校",
  155. trigger: "change"
  156. }
  157. ],
  158. examRoom: [
  159. {
  160. required: true,
  161. message: "请选择考场",
  162. trigger: "change"
  163. }
  164. ]
  165. }
  166. };
  167. },
  168. methods: {
  169. initData(val) {
  170. this.$refs.modalFormComp.resetFields();
  171. if (val.id) {
  172. this.modalForm = this.$objAssign(initModalForm, val);
  173. } else {
  174. this.modalForm = { ...initModalForm };
  175. }
  176. },
  177. visibleChange(visible) {
  178. if (visible) {
  179. this.initData(this.instance);
  180. }
  181. },
  182. areaChange() {
  183. const curArea = this.cascadeList.find(
  184. item => item.areaCode === this.modalForm.areaCode
  185. );
  186. this.schools = curArea.schools;
  187. this.rooms = [];
  188. this.modalForm.examRoom = null;
  189. if (curArea.schools.length === 1) {
  190. this.modalForm.school = curArea.schools[0].school;
  191. this.schoolChange();
  192. }
  193. },
  194. schoolChange() {
  195. const curSchool = this.schools.find(
  196. item => item.school === this.modalForm.school
  197. );
  198. this.rooms = curSchool.rooms;
  199. this.modalForm.examRoom = null;
  200. },
  201. cancel() {
  202. this.modalIsShow = false;
  203. },
  204. open() {
  205. this.modalIsShow = true;
  206. },
  207. async submit() {
  208. const valid = await this.$refs.modalFormComp.validate();
  209. if (!valid) return;
  210. if (this.isSubmit) return;
  211. this.isSubmit = true;
  212. const data = await updateStudent(this.modalForm).catch(() => {});
  213. this.isSubmit = false;
  214. if (!data) return;
  215. this.$Message.success(this.title + "成功!");
  216. this.$emit("modified");
  217. this.cancel();
  218. }
  219. }
  220. };
  221. </script>