Skip to content

Commit cd51dee

Browse files
committed
chore(export files): fixed unable export in pagination changing
1 parent db316cf commit cd51dee

4 files changed

Lines changed: 249 additions & 49 deletions

File tree

src/stores/student-store.ts

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ export const useStudentStore = defineStore('student', () => {
104104

105105
const tableData = ref<StudentRow[]>([])
106106
const selectedIds = ref<string[]>([])
107+
const selectedRowMap = ref<Record<string, StudentRow>>({})
107108
const searchForm = reactive({
108109
grade: '',
109110
name: '',
@@ -204,9 +205,32 @@ export const useStudentStore = defineStore('student', () => {
204205
])
205206

206207
const hasSelectedItems = computed(() => selectedIds.value.length > 0)
208+
const selectedStudentRows = computed(() =>
209+
selectedIds.value
210+
.map((id) => selectedRowMap.value[id])
211+
.filter((row): row is StudentRow => Boolean(row)),
212+
)
207213

208214
const handleSelectionChange = (selection: StudentRow[]) => {
209-
selectedIds.value = selection.map((student) => student.userId)
215+
const currentPageIds = new Set(tableData.value.map((student) => student.userId))
216+
217+
currentPageIds.forEach((id) => {
218+
delete selectedRowMap.value[id]
219+
})
220+
221+
selection.forEach((student) => {
222+
if (!student.userId) {
223+
return
224+
}
225+
selectedRowMap.value[student.userId] = student
226+
})
227+
228+
selectedIds.value = Object.keys(selectedRowMap.value)
229+
}
230+
231+
const clearSelectedStudents = () => {
232+
selectedIds.value = []
233+
selectedRowMap.value = {}
210234
}
211235

212236
const getList = async (
@@ -243,6 +267,7 @@ export const useStudentStore = defineStore('student', () => {
243267
searchForm.studentId = ''
244268
searchForm.major = ''
245269
searchForm.degree = ''
270+
clearSelectedStudents()
246271
}
247272

248273
const buildSearchParams = () => {
@@ -270,17 +295,20 @@ export const useStudentStore = defineStore('student', () => {
270295
}
271296

272297
const submitSearch = async () => {
298+
clearSelectedStudents()
273299
await getList(1, query.pageSize, buildSearchParams())
274300
}
275301

276-
const exportSelectedStudentInfo = async () => {
277-
if (!hasSelectedItems.value) {
302+
const exportSelectedStudentInfo = async (ids?: string[]) => {
303+
const idsToExport = ids?.length ? ids : selectedIds.value
304+
305+
if (!idsToExport.length) {
278306
ElMessage.warning('请先在列表中选择要导出的条目')
279307
return
280308
}
281309

282310
try {
283-
const response = await studentFilesApi.exportSelectedStudentInfo(selectedIds.value)
311+
const response = await studentFilesApi.exportSelectedStudentInfo(idsToExport)
284312
const blob = response.data
285313
const url = window.URL.createObjectURL(blob)
286314
const link = document.createElement('a')
@@ -395,6 +423,7 @@ export const useStudentStore = defineStore('student', () => {
395423
fieldConfigs,
396424
tableData,
397425
selectedIds,
426+
selectedStudentRows,
398427
searchForm,
399428
query,
400429
total,

src/stores/user-store.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ export const useUserStore = defineStore('user', () => {
8282
const initLoginState = async () => {
8383
initRemember()
8484
if (!(await isLoggedIn())) {
85-
clearAuth()
85+
clearAuth();
8686
}
8787
}
8888
const detectDeviceType = () => {

0 commit comments

Comments
 (0)