From 246dfad018b17c3a4b2b54bab06db4fee936d290 Mon Sep 17 00:00:00 2001 From: t8y2 <1156263951@qq.com> Date: Mon, 3 Aug 2026 16:37:42 +0800 Subject: [PATCH] fix(grid): reset infinite scroll state on database sort Closes #5172 --- apps/desktop/src/components/grid/DataGrid.vue | 8 +++-- .../DataGridInfiniteScrollSort.spec.ts | 35 +++++++++++++++++++ 2 files changed, 41 insertions(+), 2 deletions(-) create mode 100644 apps/desktop/src/components/grid/__tests__/DataGridInfiniteScrollSort.spec.ts diff --git a/apps/desktop/src/components/grid/DataGrid.vue b/apps/desktop/src/components/grid/DataGrid.vue index c89c980ec..61c1042da 100644 --- a/apps/desktop/src/components/grid/DataGrid.vue +++ b/apps/desktop/src/components/grid/DataGrid.vue @@ -4379,8 +4379,12 @@ function setDetailNull() { function applyColumnSort(column: string, columnIndex: number, direction: "asc" | "desc" | null, mode: DataGridSortMode = "database") { if (getIsResizing()) return; - currentPage.value = 1; - resetGridVerticalScroll(true); + if (mode === "database" && infiniteScrollEnabled.value) { + resetInfiniteScrollState(); + } else { + currentPage.value = 1; + resetGridVerticalScroll(true); + } if (direction) { setSort(column, columnIndex, direction, mode); if (mode === "database") { diff --git a/apps/desktop/src/components/grid/__tests__/DataGridInfiniteScrollSort.spec.ts b/apps/desktop/src/components/grid/__tests__/DataGridInfiniteScrollSort.spec.ts new file mode 100644 index 000000000..124b5dfae --- /dev/null +++ b/apps/desktop/src/components/grid/__tests__/DataGridInfiniteScrollSort.spec.ts @@ -0,0 +1,35 @@ +import { readFileSync } from "node:fs"; +import { describe, expect, it } from "vitest"; + +const dataGridSource = readFileSync(new URL("../DataGrid.vue", import.meta.url), "utf8"); + +function functionSource(name: string, nextName: string): string { + const start = dataGridSource.indexOf(`function ${name}`); + const relativeEnd = dataGridSource.slice(start).search(new RegExp(`\\n(?:async\\s+)?function\\s+${nextName}\\b`)); + expect(start).toBeGreaterThanOrEqual(0); + expect(relativeEnd).toBeGreaterThan(0); + const end = start + relativeEnd; + return dataGridSource.slice(start, end); +} + +describe("DataGrid infinite-scroll sorting", () => { + it("clears completed and pending infinite-scroll state before database sorting", () => { + const sortSource = functionSource("applyColumnSort", "selectHeaderSort"); + const resetSource = functionSource("resetInfiniteScrollState", "onToolbarRefresh"); + + expect(sortSource).toContain('if (mode === "database" && infiniteScrollEnabled.value) {\n resetInfiniteScrollState();'); + expect(resetSource).toContain("lastInfiniteScrollPage = 0;"); + expect(resetSource).toContain("infiniteScrollAllLoaded = false;"); + expect(resetSource).toContain("infiniteScrollRequestedOffset = undefined;"); + expect(resetSource).toContain("infiniteScrollRequestedLimit = undefined;"); + expect(resetSource).toContain("isInfiniteScrollPaginating.value = false;"); + expect(resetSource).toContain("infiniteScrollLoading.value = false;"); + }); + + it("preserves the existing lightweight reset for local sorting", () => { + const sortSource = functionSource("applyColumnSort", "selectHeaderSort"); + + expect(sortSource).toContain("} else {\n currentPage.value = 1;\n resetGridVerticalScroll(true);\n }"); + expect(sortSource.match(/emit\("sort"/g)).toHaveLength(1); + }); +});