import { computed, defineComponent, onMounted, reactive } from 'vue'; import styles from './index.module.less'; import { NAvatar, NButton, NCascader, NCheckbox, NCheckboxGroup, NInput, NScrollbar, NSelect, NSpace, NSpin } from 'naive-ui'; import defultHeade from '@/components/layout/images/teacherIcon.png'; import SearchInput from '/src/components/searchInput'; import { useCatchStore } from '/src/store/modules/catchData'; import { getStudentList } from '/src/views/classList/api'; import { useThrottleFn } from '@vueuse/core'; import TheEmpty from '/src/components/TheEmpty'; import { getGradeYearList } from '/src/views/home/api'; import { api_getCurrentGradeYear } from '/src/views/studentList/api'; export default defineComponent({ name: 'assign-student', props: { /** 班级列表 */ classList: { type: Array, default: () => [] }, /** 所选学生列表 */ studentList: { type: Array, default: () => [] }, /** 学年 */ currentGradeNum: { type: [String || Number], default: '' }, selectIds: { type: Array, default: () => [] }, classGroupId: { type: String, default: '' } }, emits: ['close', 'confirm'], setup(props, { emit }) { const catchStore = useCatchStore(); const state = reactive({ studentName: '', loading: false, finshed: false, // 是否加载完 checkAllStatus: false, indeterminate: false, searchFrom: { upgradeFlag: true, currentGradeNums: null as any, gradeYear: null, classGroupId: props.classGroupId || '', classInstrumentId: '', keyword: '' }, pagination: { page: 1, rows: 20, pageTotal: 0 }, tableList: [] as any, checkboxIds: [] as any, selectStudents: [] as any, selectKeyword: '', popSelectYearList: [] as any }); // 获取学年 const getYearList = async () => { try { const { data } = await api_getCurrentGradeYear({}); state.searchFrom.gradeYear = data; } catch { // } }; const getStudentLists = async () => { try { if (state.pagination.page === 1) { state.loading = true; state.tableList = []; } const { data } = await getStudentList({ ...state.searchFrom, ...state.pagination }); state.loading = false; const rows = data.rows || []; state.tableList.push(...rows); state.finshed = data.pages <= data.current ? true : false; onCheckStudents(); } catch { // state.loading = false; } }; const onSearch = () => { state.pagination.page = 1; getStudentLists(); }; const selectStudentEmpty = computed(() => { let status = true; state.selectStudents.forEach((item: any) => { if (!item.hide) { status = false; } }); return status; }); const throttledFn = useThrottleFn(() => { state.pagination.page = state.pagination.page + 1; getStudentLists(); }, 500); // 切换学生状态 const onCheckStudents = () => { if (state.tableList.length <= 0) { state.indeterminate = false; state.checkAllStatus = false; return; } // 右边数据 state.tableList.forEach((item: any) => { if (state.checkboxIds.includes(item.id)) { const index = state.selectStudents.findIndex( (select: any) => select.id == item.id ); if (index === -1) state.selectStudents.push(item); } else { const index = state.selectStudents.findIndex( (select: any) => select.id == item.id ); if(index >= 0) state.selectStudents.splice(index, 1) } }); let count = 0; state.tableList.forEach((item: any) => { const index = state.selectStudents.findIndex( (select: any) => select.id === item.id ); if(index >= 0) count++ }) if (count >= state.tableList.length) { state.checkAllStatus = true; state.indeterminate = false; } else { state.checkAllStatus = false; state.indeterminate = count === 0 ? false : true; } }; // 删除用户 const onRemove = (item: any) => { const index = state.checkboxIds.findIndex((id: any) => id == item.id); if (index !== -1) { state.checkboxIds.splice(index, 1); const sIndex = state.selectStudents.findIndex( (select: any) => select.id === item.id ); if (sIndex !== -1) { state.selectStudents.splice(sIndex, 1); } onCheckStudents(); } }; const onSave = () => { const studentInfo: any[] = []; state.selectStudents.forEach((item: any) => { studentInfo.push({ id: item.id, name: item.nickname, avatar: item.avatar }); }); emit('confirm', studentInfo); }; onMounted(async () => { console.log(props.currentGradeNum, 'props.currentGradeNum-----') if(Array.isArray(props.currentGradeNum)) { state.searchFrom.currentGradeNums = props.currentGradeNum.join(',') } else { state.searchFrom.currentGradeNums = props.currentGradeNum } state.checkboxIds = props.selectIds || []; state.loading = true; await catchStore.getSubjects(); await getYearList(); await getStudentLists(); // onCheckStudents(); // 重置选择的学生 state.selectStudents = props.studentList?.map((item: any) => { return { ...item, nickname: item.name }; }); }); return () => (
全选 ({state.tableList.length}){' '} :