ModifyStudentSimple.vue 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. <template>
  2. <el-dialog
  3. class="modify-student-simple"
  4. :visible.sync="modalIsShow"
  5. title="添加学生"
  6. top="10px"
  7. width="800px"
  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. <div class="mb-4 tab-btns">
  15. <el-button
  16. v-for="tab in tabs"
  17. :key="tab.val"
  18. size="medium"
  19. :type="curTab == tab.val ? 'primary' : 'default'"
  20. @click="selectMenu(tab.val)"
  21. >{{ tab.name }}
  22. </el-button>
  23. </div>
  24. <!-- input -->
  25. <div v-if="curTab === 'input'" class="tab-body">
  26. <el-form
  27. ref="modalFormComp"
  28. :model="modalForm"
  29. :rules="rules"
  30. :key="modalForm.id"
  31. label-position="top"
  32. >
  33. <el-form-item prop="studentName" label="姓名:">
  34. <el-input
  35. v-model.trim="modalForm.studentName"
  36. placeholder="请输入姓名"
  37. clearable
  38. ></el-input>
  39. </el-form-item>
  40. <el-form-item prop="studentCode" label="学号:">
  41. <el-input
  42. v-model.trim="modalForm.studentCode"
  43. placeholder="请输入学号"
  44. clearable
  45. ></el-input>
  46. </el-form-item>
  47. </el-form>
  48. </div>
  49. <!-- select -->
  50. <div v-if="curTab === 'select'" class="tab-body">
  51. <select-class-student
  52. ref="SelectClassStudent"
  53. v-model="selectedStudentIds"
  54. ></select-class-student>
  55. </div>
  56. <!-- import -->
  57. <div v-if="curTab === 'import'" class="tab-body">
  58. <el-button type="success" icon="el-icon-download"
  59. ><a :href="downloadUrl" :download="dfilename">模板下载</a></el-button
  60. >
  61. <upload-button
  62. btn-icon="el-icon-circle-plus-outline"
  63. btn-content="批量导入"
  64. btn-type="success"
  65. :upload-url="uploadUrl"
  66. :upload-data="uploadData"
  67. :format="['xls', 'xlsx']"
  68. accept=".xls,.xlsx"
  69. @valid-error="validError"
  70. @upload-success="uploadSuccess"
  71. >
  72. </upload-button>
  73. </div>
  74. <div v-if="!IS_IMPORT" slot="footer">
  75. <el-button type="primary" :disabled="isSubmit" @click="submit"
  76. >确认</el-button
  77. >
  78. <el-button @click="cancel">取消</el-button>
  79. </div>
  80. <div v-else slot="footer"></div>
  81. </el-dialog>
  82. </template>
  83. <script>
  84. import { updateStudentSimple, batchAddStudentSimple } from "../api";
  85. import UploadButton from "../../../components/UploadButton";
  86. import SelectClassStudent from "./SelectClassStudent";
  87. const initModalForm = {
  88. id: null,
  89. teachClazzId: null,
  90. studentName: "",
  91. studentCode: ""
  92. };
  93. export default {
  94. name: "modify-student-simple",
  95. components: { UploadButton, SelectClassStudent },
  96. props: {
  97. instance: {
  98. type: Object,
  99. default() {
  100. return {};
  101. }
  102. }
  103. },
  104. computed: {
  105. IS_IMPORT() {
  106. return this.curTab === "import";
  107. }
  108. },
  109. data() {
  110. return {
  111. modalIsShow: false,
  112. isSubmit: false,
  113. curTab: "select",
  114. tabs: [
  115. {
  116. name: "手动添加",
  117. val: "input"
  118. },
  119. {
  120. name: "从课程班级添加",
  121. val: "select"
  122. },
  123. {
  124. name: "批量导入",
  125. val: "import"
  126. }
  127. ],
  128. modalForm: { ...initModalForm },
  129. rules: {
  130. studentName: [
  131. {
  132. required: true,
  133. message: "请输入姓名",
  134. trigger: "change"
  135. },
  136. {
  137. message: "姓名不能超过50个字",
  138. max: 50,
  139. trigger: "change"
  140. }
  141. ],
  142. studentCode: [
  143. {
  144. required: true,
  145. message: "请输入学号",
  146. trigger: "change"
  147. },
  148. {
  149. message: "学号不能超过50个字",
  150. max: 50,
  151. trigger: "change"
  152. }
  153. ]
  154. },
  155. selectedStudentIds: [],
  156. // import
  157. uploadData: {},
  158. uploadUrl: "/api/admin/teach/student/import",
  159. downloadUrl: "/temps/clazzSimpleStudentTemplate.xlsx",
  160. dfilename: "教学班级学生导入模板.xlsx"
  161. };
  162. },
  163. methods: {
  164. initData(val) {
  165. this.modalForm = this.$objAssign(initModalForm, val);
  166. this.uploadData = {
  167. teachClazzId: val.teachClazzId
  168. };
  169. },
  170. visibleChange() {
  171. this.initData(this.instance);
  172. },
  173. cancel() {
  174. this.modalIsShow = false;
  175. },
  176. open() {
  177. this.modalIsShow = true;
  178. },
  179. selectMenu(tab) {
  180. this.curTab = tab;
  181. },
  182. submit() {
  183. const submitFunc = {
  184. input: this.submitInput,
  185. select: this.submitSelect
  186. };
  187. submitFunc[this.curTab]();
  188. },
  189. async submitInput() {
  190. const valid = await this.$refs.modalFormComp.validate().catch(() => {});
  191. if (!valid) return;
  192. if (this.isSubmit) return;
  193. this.isSubmit = true;
  194. const data = await updateStudentSimple(this.modalForm).catch(() => {});
  195. this.isSubmit = false;
  196. if (!data) return;
  197. this.$message.success("添加成功!");
  198. this.$emit("modified");
  199. this.modalForm = this.$objAssign(initModalForm, this.instance);
  200. },
  201. async submitSelect() {
  202. if (!this.selectedStudentIds.length) {
  203. this.$message.error("请选择学生");
  204. return;
  205. }
  206. if (this.isSubmit) return;
  207. this.isSubmit = true;
  208. const data = await batchAddStudentSimple({
  209. teachClazzId: this.instance.teachClazzId,
  210. basicStudentIdSet: this.selectedStudentIds
  211. }).catch(() => {});
  212. this.isSubmit = false;
  213. if (!data) return;
  214. this.$message.success("添加成功!");
  215. this.$emit("modified");
  216. this.selectedStudentIds = [];
  217. this.$refs.SelectClassStudent.clearSelection();
  218. },
  219. // import
  220. validError(errorData) {
  221. this.$message.error(errorData.message);
  222. },
  223. uploadSuccess(data) {
  224. this.$message.success(data.data || "学生导入成功!");
  225. this.$emit("modified");
  226. this.cancel();
  227. }
  228. }
  229. };
  230. </script>