index.tsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. import { computed, defineComponent, onMounted, reactive } from 'vue';
  2. import styles from './index.module.less';
  3. import {
  4. NAvatar,
  5. NButton,
  6. NCascader,
  7. NCheckbox,
  8. NCheckboxGroup,
  9. NInput,
  10. NScrollbar,
  11. NSelect,
  12. NSpace,
  13. NSpin
  14. } from 'naive-ui';
  15. import defultHeade from '@/components/layout/images/teacherIcon.png';
  16. import SearchInput from '/src/components/searchInput';
  17. import { useCatchStore } from '/src/store/modules/catchData';
  18. import { getStudentList } from '/src/views/classList/api';
  19. import { useThrottleFn } from '@vueuse/core';
  20. import TheEmpty from '/src/components/TheEmpty';
  21. import { getGradeYearList } from '/src/views/home/api';
  22. import { api_getCurrentGradeYear } from '/src/views/studentList/api';
  23. export default defineComponent({
  24. name: 'assign-student',
  25. props: {
  26. /** 班级列表 */
  27. classList: {
  28. type: Array,
  29. default: () => []
  30. },
  31. /** 所选学生列表 */
  32. studentList: {
  33. type: Array,
  34. default: () => []
  35. },
  36. /** 学年 */
  37. currentGradeNum: {
  38. type: [String || Number],
  39. default: ''
  40. },
  41. selectIds: {
  42. type: Array,
  43. default: () => []
  44. },
  45. classGroupId: {
  46. type: String,
  47. default: ''
  48. }
  49. },
  50. emits: ['close', 'confirm'],
  51. setup(props, { emit }) {
  52. const catchStore = useCatchStore();
  53. const state = reactive({
  54. studentName: '',
  55. loading: false,
  56. finshed: false, // 是否加载完
  57. checkAllStatus: false,
  58. indeterminate: false,
  59. searchFrom: {
  60. upgradeFlag: true,
  61. currentGradeNum: props.currentGradeNum || '',
  62. gradeYear: null,
  63. classGroupId: props.classGroupId || '',
  64. subjectId: '',
  65. keyword: ''
  66. },
  67. pagination: {
  68. page: 1,
  69. rows: 20,
  70. pageTotal: 0
  71. },
  72. tableList: [] as any,
  73. checkboxIds: [] as any,
  74. selectStudents: [] as any,
  75. selectKeyword: '',
  76. popSelectYearList: [] as any
  77. });
  78. // 获取学年
  79. const getYearList = async () => {
  80. try {
  81. const { data } = await api_getCurrentGradeYear({});
  82. state.searchFrom.gradeYear = data;
  83. // const { data } = await getGradeYearList();
  84. // const temp = data || [];
  85. // temp.forEach((i: any) => {
  86. // i.name = i.name + '学年';
  87. // });
  88. // state.popSelectYearList = temp || [];
  89. // if (temp.length > 0 && !state.searchFrom.gradeYear) {
  90. // state.searchFrom.gradeYear = temp[0].id;
  91. // }
  92. } catch {
  93. //
  94. }
  95. };
  96. const getStudentLists = async () => {
  97. try {
  98. if (state.pagination.page === 1) {
  99. state.loading = true;
  100. state.tableList = [];
  101. }
  102. const { data } = await getStudentList({
  103. ...state.searchFrom,
  104. ...state.pagination
  105. });
  106. state.loading = false;
  107. const rows = data.rows || [];
  108. state.tableList.push(...rows);
  109. state.finshed = data.pages <= data.current ? true : false;
  110. onCheckStudents();
  111. } catch {
  112. //
  113. state.loading = false;
  114. }
  115. };
  116. const onSearch = () => {
  117. state.pagination.page = 1;
  118. getStudentLists();
  119. };
  120. const selectStudentEmpty = computed(() => {
  121. let status = true;
  122. state.selectStudents.forEach((item: any) => {
  123. if (!item.hide) {
  124. status = false;
  125. }
  126. });
  127. return status;
  128. });
  129. const throttledFn = useThrottleFn(() => {
  130. state.pagination.page = state.pagination.page + 1;
  131. getStudentLists();
  132. }, 500);
  133. // 切换学生状态
  134. const onCheckStudents = () => {
  135. // state.selectStudents = [];
  136. if (state.checkboxIds.length <= 0 || state.tableList.length <= 0) {
  137. state.indeterminate = false;
  138. state.checkAllStatus = false;
  139. return;
  140. }
  141. let count = 0;
  142. // 右边数据
  143. state.tableList.forEach((item: any) => {
  144. if (state.checkboxIds.includes(item.id)) {
  145. count++;
  146. const index = state.selectStudents.findIndex(
  147. (select: any) => select.id === item.id
  148. );
  149. if (index === -1) state.selectStudents.push(item);
  150. }
  151. });
  152. if (count >= state.tableList.length) {
  153. state.checkAllStatus = true;
  154. state.indeterminate = false;
  155. } else {
  156. state.checkAllStatus = false;
  157. state.indeterminate = true;
  158. }
  159. };
  160. // 删除用户
  161. const onRemove = (item: any) => {
  162. const index = state.checkboxIds.findIndex((id: any) => id === item.id);
  163. if (index !== -1) {
  164. state.checkboxIds.splice(index, 1);
  165. const sIndex = state.selectStudents.findIndex(
  166. (select: any) => select.id === item.id
  167. );
  168. if (sIndex !== -1) {
  169. state.selectStudents.splice(sIndex, 1);
  170. }
  171. onCheckStudents();
  172. }
  173. };
  174. const onSave = () => {
  175. const studentInfo: any[] = [];
  176. state.selectStudents.forEach((item: any) => {
  177. studentInfo.push({
  178. id: item.id,
  179. name: item.nickname,
  180. avatar: item.avatar
  181. });
  182. });
  183. emit('confirm', studentInfo);
  184. };
  185. onMounted(async () => {
  186. state.checkboxIds = props.selectIds || [];
  187. state.loading = true;
  188. await catchStore.getSubjects();
  189. await getYearList();
  190. await getStudentLists();
  191. // onCheckStudents();
  192. // 重置选择的学生
  193. state.selectStudents = props.studentList?.map((item: any) => {
  194. return {
  195. ...item,
  196. nickname: item.name
  197. };
  198. });
  199. });
  200. return () => (
  201. <div class={styles.assignStudent}>
  202. <div class={styles.studentListGroup}>
  203. <div class={styles.searchSection}>
  204. <div class={styles.searchSpace}>
  205. <NSelect
  206. placeholder="全部班级"
  207. disabled={props.classGroupId ? true : false}
  208. v-model:value={state.searchFrom.classGroupId}
  209. onUpdate:value={() => onSearch()}
  210. options={
  211. [{ label: '全部班级', value: '' }, ...props.classList] as any
  212. }
  213. />
  214. {/* <NSelect
  215. options={[
  216. { label: '全部乐器', value: '' },
  217. ...catchStore.getEnableSubjects
  218. ]}
  219. placeholder="全部乐器"
  220. v-model:value={state.searchFrom.subjectId}
  221. onUpdate:value={() => onSearch()}
  222. /> */}
  223. <NCascader
  224. options={[
  225. { name: '全部乐器', id: '' },
  226. ...catchStore.getSubjectList
  227. ]}
  228. placeholder="全部乐器"
  229. v-model:value={state.searchFrom.subjectId}
  230. onUpdate:value={() => onSearch()}
  231. checkStrategy="child"
  232. showPath
  233. childrenField="instruments"
  234. expandTrigger="hover"
  235. labelField="name"
  236. valueField="id"
  237. clearable
  238. filterable
  239. />
  240. </div>
  241. <SearchInput
  242. {...{ placeholder: '请输入学生姓名/手机号' }}
  243. class={styles.searchInput}
  244. searchWord={state.searchFrom.keyword}
  245. onChangeValue={(val: string) => {
  246. state.searchFrom.keyword = val;
  247. }}
  248. onClear={() => {
  249. state.searchFrom.keyword = '';
  250. onSearch();
  251. }}
  252. onKeyup={(e: KeyboardEvent) => {
  253. if (e.code === 'Enter') {
  254. onSearch();
  255. }
  256. }}></SearchInput>
  257. </div>
  258. <div class={styles.studentSection}>
  259. <div class={styles.checkboxAll}>
  260. <NCheckbox
  261. v-model:checked={state.checkAllStatus}
  262. indeterminate={state.indeterminate}
  263. onUpdate:checked={(val: any) => {
  264. if (val) {
  265. const ids: any = [];
  266. state.tableList.forEach((item: any) => {
  267. ids.push(item.id);
  268. });
  269. state.checkboxIds = ids;
  270. } else {
  271. state.checkboxIds = [];
  272. state.selectStudents = [];
  273. state.indeterminate = false;
  274. }
  275. onCheckStudents();
  276. }}></NCheckbox>
  277. <p>
  278. 全选 <span class={styles.nums}>({state.tableList.length})</span>{' '}
  279. </p>
  280. </div>
  281. </div>
  282. <NScrollbar
  283. class={styles.student}
  284. onScroll={(e: any) => {
  285. const clientHeight = e.target?.clientHeight;
  286. const scrollTop = e.target?.scrollTop;
  287. const scrollHeight = e.target?.scrollHeight;
  288. // 是否到底,是否加载完
  289. if (
  290. clientHeight + scrollTop + 20 >= scrollHeight &&
  291. !state.finshed &&
  292. !state.loading
  293. ) {
  294. throttledFn();
  295. }
  296. }}>
  297. <NSpin show={state.loading} class={styles.loadingSection}>
  298. <NCheckboxGroup
  299. v-model:value={state.checkboxIds}
  300. onUpdate:value={() => {
  301. state.selectStudents = [];
  302. onCheckStudents();
  303. }}>
  304. {state.tableList.map((item: any) => (
  305. <NCheckbox value={item.id} class={[styles.studentItem]}>
  306. <div class={styles.studentInfo}>
  307. <NAvatar
  308. src={item.avatar || defultHeade}
  309. class={styles.studentImg}
  310. />
  311. <div class={styles.studentValue}>
  312. <div class={styles.userInfo}>
  313. <span class={styles.name}>{item.nickname}</span>
  314. {item.membership && <i class={styles.iconMember}></i>}
  315. {item.classGroupName && (
  316. <span class={styles.className}>
  317. {item.classGroupName}
  318. </span>
  319. )}
  320. </div>
  321. <div class={styles.phone}>{item.phone}</div>
  322. </div>
  323. </div>
  324. </NCheckbox>
  325. ))}
  326. </NCheckboxGroup>
  327. {state.tableList.length <= 0 && !state.loading && <TheEmpty />}
  328. </NSpin>
  329. </NScrollbar>
  330. </div>
  331. <div class={styles.selectStudentGroup}>
  332. <div class={styles.selectCount}>
  333. 当前选中 <span>({state.selectStudents.length}) </span>:
  334. </div>
  335. <div class={styles.searchSection}>
  336. <SearchInput
  337. {...{ placeholder: '请输入学生姓名' }}
  338. class={styles.searchInput}
  339. searchWord={state.selectKeyword}
  340. onChangeValue={(val: string) => {
  341. state.selectKeyword = val;
  342. state.selectStudents.forEach((item: any) => {
  343. if (item.nickname?.indexOf(val) === -1) {
  344. item.hide = true;
  345. } else {
  346. item.hide = false;
  347. }
  348. });
  349. }}></SearchInput>
  350. </div>
  351. <NScrollbar class={styles.student}>
  352. {state.selectStudents.map((student: any) => (
  353. <div class={[styles.studentItem, student.hide && styles.hide]}>
  354. <div class={styles.studentInfo}>
  355. <NAvatar
  356. src={student.avatar || defultHeade}
  357. class={styles.studentImg}
  358. />
  359. <span class={styles.name}>{student.nickname}</span>
  360. </div>
  361. <i
  362. class={styles.iconClose}
  363. onClick={() => onRemove(student)}></i>
  364. </div>
  365. ))}
  366. {selectStudentEmpty.value && <TheEmpty />}
  367. </NScrollbar>
  368. <NSpace justify="end" class={styles.btnGroup}>
  369. <NButton type="default" onClick={() => emit('close')}>
  370. 取消
  371. </NButton>
  372. <NButton type="primary" onClick={onSave}>
  373. 保存
  374. </NButton>
  375. </NSpace>
  376. </div>
  377. </div>
  378. );
  379. }
  380. });