ModifyExamTaskStudent.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. <template>
  2. <el-dialog
  3. class="select-user-dialog"
  4. :visible.sync="modalIsShow"
  5. title="选择考试对象"
  6. top="10vh"
  7. width="900px"
  8. :close-on-click-modal="false"
  9. :close-on-press-escape="false"
  10. append-to-body
  11. @opened="visibleChange"
  12. >
  13. <div class="user-search">
  14. <el-form inline>
  15. <!-- <el-form-item>
  16. <el-select v-model="filter.courseId" placeholder="请选择" filterable>
  17. <el-option-group
  18. v-for="item in courses"
  19. :key="item.label"
  20. :label="item.label"
  21. >
  22. <el-option
  23. v-for="course in item.options"
  24. :key="course.id"
  25. :value="course.id"
  26. :label="`${course.name}(${course.code})`"
  27. >
  28. </el-option>
  29. </el-option-group>
  30. </el-select>
  31. </el-form-item> -->
  32. <el-form-item>
  33. <el-input
  34. v-model.trim="filter.teacher"
  35. placeholder="任课老师"
  36. clearable
  37. style="width: 160px"
  38. ></el-input>
  39. </el-form-item>
  40. <el-form-item>
  41. <el-input
  42. v-model.trim="filter.teachClassName"
  43. placeholder="班级名称(教学班)"
  44. clearable
  45. style="width: 160px"
  46. ></el-input>
  47. </el-form-item>
  48. <el-form-item>
  49. <el-input
  50. v-model.trim="filter.className"
  51. placeholder="班级名称(行政班)"
  52. clearable
  53. style="width: 160px"
  54. ></el-input>
  55. </el-form-item>
  56. <el-form-item>
  57. <el-input
  58. v-model.trim="filter.paperNumber"
  59. placeholder="试卷编号"
  60. clearable
  61. style="width: 160px"
  62. ></el-input>
  63. </el-form-item>
  64. <el-form-item>
  65. <el-button type="primary" @click="search">查询</el-button>
  66. </el-form-item>
  67. </el-form>
  68. </div>
  69. <el-row type="flex" :gutter="10">
  70. <el-col :span="12">
  71. <h3 class="user-part-title title-select">
  72. <el-checkbox
  73. v-model="allSelect"
  74. class="title-checkbox"
  75. @change="allSelectChange"
  76. >全选</el-checkbox
  77. >
  78. <div>{{ showStudent ? "班级/学生" : "班级" }}</div>
  79. </h3>
  80. <div class="user-tree">
  81. <el-tree
  82. ref="UserTree"
  83. :data="userTree"
  84. node-key="id"
  85. show-checkbox
  86. check-on-click-node
  87. :expand-on-click-node="false"
  88. :props="defaultProps"
  89. @check="checkChange"
  90. >
  91. <span class="custom-common-node" slot-scope="{ data }">
  92. {{ data.label }}
  93. <i v-if="data.count !== undefined" class="color-gray-3 ml-1"
  94. >[{{ data.count }}]</i
  95. >
  96. </span>
  97. </el-tree>
  98. </div>
  99. </el-col>
  100. <el-col :span="12">
  101. <h3 class="user-part-title">已选范围</h3>
  102. <div class="user-tree">
  103. <el-tree
  104. ref="UserSelectedTree"
  105. :data="selectedUsers"
  106. node-key="id"
  107. default-expand-all
  108. :props="defaultProps"
  109. >
  110. <span class="custom-tree-node" slot-scope="{ node, data }">
  111. <span>
  112. {{ data.label }}
  113. <i v-if="data.count !== undefined" class="color-gray-3 ml-1">
  114. [{{ data.count }}]
  115. </i>
  116. </span>
  117. <el-button
  118. type="text"
  119. icon="el-icon-remove"
  120. class="color-danger"
  121. @click.stop="removeSelectStudent(node)"
  122. >
  123. </el-button>
  124. </span>
  125. </el-tree>
  126. </div>
  127. </el-col>
  128. </el-row>
  129. <div slot="footer">
  130. <el-button type="primary" :disabled="loading" @click="submit"
  131. >确认</el-button
  132. >
  133. <el-button @click="cancel">取消</el-button>
  134. </div>
  135. </el-dialog>
  136. </template>
  137. <script>
  138. import { uploadOrFindExamTaskStudent } from "../../api";
  139. import { courseQuery } from "../../../base/api";
  140. export default {
  141. name: "modify-exam-task-student",
  142. props: {
  143. disabledIds: {
  144. type: Array,
  145. default() {
  146. return [];
  147. },
  148. },
  149. selectedIds: {
  150. type: Array,
  151. default() {
  152. return [];
  153. },
  154. },
  155. filterParams: {
  156. type: Object,
  157. default() {
  158. return {
  159. courseId: "",
  160. examId: "",
  161. teachingRoomId: "",
  162. };
  163. },
  164. },
  165. showStudent: {
  166. type: Boolean,
  167. default: false,
  168. },
  169. },
  170. data() {
  171. return {
  172. modalIsShow: false,
  173. filter: {
  174. courseId: "",
  175. examId: "",
  176. teacher: "",
  177. teachClassName: "",
  178. className: "",
  179. paperNumber: "",
  180. },
  181. dataTree: [],
  182. userTree: [],
  183. selectedUsers: [],
  184. filterLabel: "",
  185. loading: false,
  186. courses: [],
  187. allSelect: false,
  188. defaultProps: {
  189. children: "children",
  190. label: "label",
  191. disabled: "disabled",
  192. },
  193. };
  194. },
  195. methods: {
  196. async visibleChange() {
  197. this.allSelect = false;
  198. this.filter = this.$objAssign(this.filter, this.filterParams);
  199. await this.getStudents();
  200. this.$refs.UserTree.setCheckedKeys([]);
  201. this.selectedUsers = [];
  202. this.userTree = this.getUserTree();
  203. this.updateUserTreeDisableInfo(this.disabledIds);
  204. if (this.selectedIds && this.selectedIds.length) {
  205. this.$nextTick(() => {
  206. this.$refs.UserTree.setCheckedKeys(this.selectedIds);
  207. this.checkChange();
  208. });
  209. }
  210. },
  211. async search() {
  212. await this.getStudents();
  213. this.userTree = this.getUserTree();
  214. this.$nextTick(() => {
  215. this.setTreeSelectedKeys();
  216. });
  217. },
  218. async getCourses() {
  219. this.courses = [];
  220. if (!this.filterParams.teachingRoomId) return;
  221. const res = await courseQuery({
  222. teachingRoomId: this.filterParams.teachingRoomId,
  223. examId: this.filterParams.examId,
  224. });
  225. const teachingCourseIds = res.teachCourseList.map((item) => item.id);
  226. this.courses = [
  227. {
  228. label: "课程管理",
  229. options: res.teachCourseList,
  230. },
  231. {
  232. label: "开课学院",
  233. options: res.basicCourseList.filter(
  234. (item) => !teachingCourseIds.includes(item.id)
  235. ),
  236. },
  237. ];
  238. },
  239. async getStudents() {
  240. const data = await uploadOrFindExamTaskStudent(this.filter);
  241. this.dataTree = this.parseStudentData(data);
  242. },
  243. parseStudentData(data) {
  244. const disabledStdIds = this.disabledIds.map((item) => item.split("_")[1]);
  245. return data.map((item) => {
  246. let nitem = {
  247. id: item.teachClassName,
  248. label: item.teachClassName,
  249. children: [],
  250. };
  251. nitem.children = item.studentInfoList.map((elem) => {
  252. return {
  253. ...elem,
  254. label: elem.studentName,
  255. isUser: true,
  256. disabled: disabledStdIds.includes(elem.studentId),
  257. };
  258. });
  259. nitem.disabled = !nitem.children.some((elem) => !elem.disabled);
  260. nitem.count = nitem.children.length;
  261. return nitem;
  262. });
  263. },
  264. getUserTree() {
  265. let userTree = this.dataTree;
  266. if (this.showStudent) return userTree;
  267. return userTree.map((item) => {
  268. let nitem = { ...item };
  269. nitem.children = [];
  270. return nitem;
  271. });
  272. },
  273. allSelectChange() {
  274. if (this.allSelect) {
  275. this.$refs.UserTree.setCheckedKeys(
  276. this.userTree.map((item) => item.id)
  277. );
  278. } else {
  279. this.$refs.UserTree.setCheckedKeys([]);
  280. }
  281. this.checkChange();
  282. },
  283. checkChange() {
  284. // console.log("check click");
  285. const selectedKeys = this.$refs.UserTree.getCheckedKeys(true);
  286. if (!this.showStudent) {
  287. this.selectedUsers = this.userTree.filter((item) =>
  288. selectedKeys.includes(item.id)
  289. );
  290. return;
  291. }
  292. let selectedUsers = [];
  293. this.userTree.forEach((item) => {
  294. const children = item.children.filter((elem) =>
  295. selectedKeys.includes(elem.id)
  296. );
  297. if (children.length) {
  298. selectedUsers.push({
  299. ...item,
  300. children,
  301. });
  302. }
  303. });
  304. this.selectedUsers = selectedUsers;
  305. },
  306. removeSelectStudent(node) {
  307. // console.log("node remove");
  308. if (!this.showStudent) {
  309. this.selectedUsers = this.selectedUsers.filter(
  310. (item) => item.id !== node.data.id
  311. );
  312. this.$refs.UserTree.setCheckedKeys(
  313. this.selectedUsers.map((item) => item.id)
  314. );
  315. return;
  316. }
  317. this.$refs.UserSelectedTree.remove(node);
  318. this.setTreeSelectedKeys();
  319. this.checkChange();
  320. },
  321. setTreeSelectedKeys() {
  322. if (!this.showStudent) {
  323. const selectedUsersIds = this.selectedUsers.map((item) => item.id);
  324. this.$refs.UserTree.setCheckedKeys(selectedUsersIds);
  325. return;
  326. }
  327. let selectedUserIds = [];
  328. this.selectedUsers.forEach((item) => {
  329. item.children.forEach((elem) => {
  330. selectedUserIds.push(elem.id);
  331. });
  332. });
  333. this.$refs.UserTree.setCheckedKeys(selectedUserIds);
  334. },
  335. labelChange() {
  336. if (this.filterLabel) {
  337. const escapeRegexpString = (value = "") =>
  338. String(value).replace(/[|\\{}()[\]^$+*?.]/g, "\\$&");
  339. const reg = new RegExp(escapeRegexpString(this.filterLabel), "i");
  340. this.userTree = this.getUserTree().filter((item) =>
  341. reg.test(item.label)
  342. );
  343. } else {
  344. this.userTree = this.getUserTree();
  345. }
  346. this.setTreeSelectedKeys();
  347. },
  348. updateUserTreeDisableInfo(disabledIds) {
  349. if (!this.showStudent) {
  350. const disabledClassIds = [];
  351. this.dataTree.forEach((item) => {
  352. if (item.children.some((elem) => disabledIds.includes(elem.id))) {
  353. disabledClassIds.push(item.id);
  354. }
  355. });
  356. this.userTree.forEach((item) => {
  357. item.disabled = disabledClassIds.includes(item.id);
  358. });
  359. return;
  360. }
  361. this.userTree.forEach((item) => {
  362. item.children.forEach((elem) => {
  363. elem.disabled = disabledIds.includes(elem.id);
  364. });
  365. item.disabled = !item.children.some((elem) => !elem.disabled);
  366. });
  367. },
  368. cancel() {
  369. this.modalIsShow = false;
  370. },
  371. open() {
  372. this.modalIsShow = true;
  373. },
  374. getFullSelectedUsers() {
  375. let userTree = this.dataTree;
  376. const selectedUsersIds = this.selectedUsers.map((item) => item.id);
  377. return userTree.filter((item) => selectedUsersIds.includes(item.id));
  378. },
  379. submit() {
  380. if (!this.selectedUsers.length) {
  381. this.$message.error("请选择考试对象");
  382. return;
  383. }
  384. if (this.loading) return;
  385. this.loading = true;
  386. const selectedUsers = this.showStudent
  387. ? this.selectedUsers
  388. : this.getFullSelectedUsers();
  389. this.$emit("modified", {
  390. selectedStudents: selectedUsers,
  391. isSelectStudent: this.showStudent,
  392. });
  393. setTimeout(() => {
  394. this.loading = false;
  395. }, 500);
  396. this.cancel();
  397. },
  398. },
  399. };
  400. </script>