Skip to content

armgit / dbx

Public Imported from GitHub
0

25 MB lightweight cross-platform database client for 100+ databases, including MySQL, PostgreSQL, SQLite, Redis, MongoDB, DuckDB, SQL Server, and Dameng. Built-in AI, MCP Server, CLI, desktop and Docker. | 轻量级跨平台数据库管理工具,支持 MySQL、PostgreSQL、SQLite、Redis、MongoDB、达梦等 100+ 数据库,提供桌面端、Docker、CLI、内置 AI 助手和 MCP。

fix(data-grid): preserve header sort state

t8y24a33cd836c85
diff --git a/apps/desktop/src/components/grid/DataGrid.vue b/apps/desktop/src/components/grid/DataGrid.vue
index 39de555f5f..7ccc08064a 100644
--- a/apps/desktop/src/components/grid/DataGrid.vue
+++ b/apps/desktop/src/components/grid/DataGrid.vue
@@ -173,6 +173,7 @@ import { useSqlHighlighter } from "@/composables/useSqlHighlighter";
import { useCellDetailEditor, type UseCellDetailEditorReturn } from "@/composables/useCellDetailEditor";
import { useTheme } from "@/composables/useTheme";
import { useSettingsStore } from "@/stores/settingsStore";
+import { nextDataGridSortState, type DataGridSortDirection } from "@/lib/dataGridSort";
const { t } = useI18n();
const settingsStore = useSettingsStore();
@@ -199,6 +200,9 @@ const props = defineProps<{
sourceColumns?: Array<string | undefined>;
queryEditabilityReason?: string;
initialWhereInput?: string;
+ sortColumn?: string;
+ sortColumnIndex?: number;
+ sortDirection?: DataGridSortDirection;
tableMeta?: {
schema?: string;
tableName: string;
@@ -399,7 +403,7 @@ const transposeScrollLeft = ref(0);
const transposeViewportWidth = ref(0);
const sortCol = ref<string | null>(null);
const sortColIndex = ref<number | null>(null);
-const sortDir = ref<"asc" | "desc">("asc");
+const sortDir = ref<DataGridSortDirection>("asc");
const searchText = ref("");
const deferredClientSearchText = ref("");
const searchOverlayVisible = ref(false);
@@ -1753,6 +1757,17 @@ function syncOrderByInputWithSort(column: string | null, direction: "asc" | "des
orderByInput.value = column && direction ? `${queryColumnRef(column)} ${direction.toUpperCase()}` : "";
}
+watch(
+ () => [props.sortColumn, props.sortColumnIndex, props.sortDirection] as const,
+ ([column, columnIndex, direction]) => {
+ sortCol.value = column && direction ? column : null;
+ sortColIndex.value = typeof columnIndex === "number" && direction ? columnIndex : null;
+ sortDir.value = direction ?? "asc";
+ syncOrderByInputWithSort(sortCol.value, sortCol.value ? sortDir.value : null);
+ },
+ { immediate: true },
+);
+
function firstPage() {
if (currentPage.value <= 1) return;
currentPage.value = 1;
@@ -2671,25 +2686,16 @@ function toggleSort(colName: string, colIdx: number) {
if (getIsResizing()) return;
currentPage.value = 1;
resetGridVerticalScroll(true);
- if (sortCol.value === colName && sortColIndex.value === colIdx) {
- if (sortDir.value === "asc") {
- sortDir.value = "desc";
- syncOrderByInputWithSort(colName, "desc");
- emit("sort", colName, colIdx, "desc", currentWhereInput());
- } else {
- sortCol.value = null;
- sortColIndex.value = null;
- sortDir.value = "asc";
- syncOrderByInputWithSort(null, null);
- emit("sort", colName, colIdx, null, currentWhereInput());
- }
- } else {
- sortCol.value = colName;
- sortColIndex.value = colIdx;
- sortDir.value = "asc";
- syncOrderByInputWithSort(colName, "asc");
- emit("sort", colName, colIdx, "asc", currentWhereInput());
- }
+ const next = nextDataGridSortState(
+ { column: sortCol.value, columnIndex: sortColIndex.value, direction: sortDir.value },
+ colName,
+ colIdx,
+ );
+ sortCol.value = next.column;
+ sortColIndex.value = next.columnIndex;
+ sortDir.value = next.direction;
+ syncOrderByInputWithSort(next.column, next.column ? next.direction : null);
+ emit("sort", colName, colIdx, next.column ? next.direction : null, currentWhereInput());
}
function applyContextSort(direction: "asc" | "desc" | null) {
diff --git a/apps/desktop/src/components/layout/ContentArea.vue b/apps/desktop/src/components/layout/ContentArea.vue
index 94a97a1be1..203339f6e7 100644
--- a/apps/desktop/src/components/layout/ContentArea.vue
+++ b/apps/desktop/src/components/layout/ContentArea.vue
@@ -446,6 +446,9 @@ defineExpose({ focusSearch, refreshData, handleModRTarget });
:cache-key="`${activeTab.id}-${activeTab.activeResultIndex ?? 0}`"
class="flex-1 min-h-0"
:result="activeTab.result"
+ :sort-column="activeTab.resultSortColumn"
+ :sort-column-index="activeTab.resultSortColumnIndex"
+ :sort-direction="activeTab.resultSortDirection"
:sql="activeTab.lastExecutedSql || activeTab.sql"
:loading="activeTab.isExecuting"
:editable="!!activeTab.queryAnalysis"
@@ -649,6 +652,9 @@ defineExpose({ focusSearch, refreshData, handleModRTarget });
:key="activeTab.id"
:cache-key="activeTab.id"
:result="activeTab.result"
+ :sort-column="activeTab.resultSortColumn"
+ :sort-column-index="activeTab.resultSortColumnIndex"
+ :sort-direction="activeTab.resultSortDirection"
:sql="activeTab.sql"
:loading="activeTab.isExecuting"
:editable="isTableDataEditable(activeConnection?.db_type, activeTab.tableMeta?.primaryKeys ?? [])"
diff --git a/apps/desktop/src/composables/useDataGridActions.ts b/apps/desktop/src/composables/useDataGridActions.ts
index 55129cf623..e1a64b07ea 100644
--- a/apps/desktop/src/composables/useDataGridActions.ts
+++ b/apps/desktop/src/composables/useDataGridActions.ts
@@ -117,6 +117,9 @@ export function useDataGridActions(activeTab: ComputedRef<QueryTab | undefined>)
async function onSort(column: string, columnIndex: number, direction: "asc" | "desc" | null, whereInput?: string) {
const tab = activeTab.value;
if (!tab) return;
+ tab.resultSortColumn = direction ? column : undefined;
+ tab.resultSortColumnIndex = direction ? columnIndex : undefined;
+ tab.resultSortDirection = direction ?? undefined;
if (tab.mode === "data") {
if (!tab.tableMeta) return;
diff --git a/apps/desktop/src/lib/dataGridSort.ts b/apps/desktop/src/lib/dataGridSort.ts
new file mode 100644
index 0000000000..6140b0da5b
--- /dev/null
+++ b/apps/desktop/src/lib/dataGridSort.ts
@@ -0,0 +1,21 @@
+export type DataGridSortDirection = "asc" | "desc";
+
+export interface DataGridSortState {
+ column: string | null;
+ columnIndex: number | null;
+ direction: DataGridSortDirection;
+}
+
+export function nextDataGridSortState(
+ current: DataGridSortState,
+ column: string,
+ columnIndex: number,
+): DataGridSortState {
+ if (current.column === column && current.columnIndex === columnIndex) {
+ if (current.direction === "asc") {
+ return { column, columnIndex, direction: "desc" };
+ }
+ return { column: null, columnIndex: null, direction: "asc" };
+ }
+ return { column, columnIndex, direction: "asc" };
+}
diff --git a/apps/desktop/src/types/database.ts b/apps/desktop/src/types/database.ts
index 408d60864b..6a3de06c0d 100644
--- a/apps/desktop/src/types/database.ts
+++ b/apps/desktop/src/types/database.ts
@@ -329,6 +329,9 @@ export interface QueryTab {
lastExecutedSql?: string;
resultBaseSql?: string;
resultSortedSql?: string;
+ resultSortColumn?: string;
+ resultSortColumnIndex?: number;
+ resultSortDirection?: "asc" | "desc";
resultPageSql?: string;
resultPageLimit?: number;
resultPageOffset?: number;
diff --git a/packages/app-tests/dataGridSort.test.ts b/packages/app-tests/dataGridSort.test.ts
new file mode 100644
index 0000000000..2367c2cc9a
--- /dev/null
+++ b/packages/app-tests/dataGridSort.test.ts
@@ -0,0 +1,20 @@
+import { strict as assert } from "node:assert";
+import test from "node:test";
+import { nextDataGridSortState } from "../../apps/desktop/src/lib/dataGridSort.ts";
+
+test("cycles data grid header sort through asc, desc, and clear", () => {
+ const asc = nextDataGridSortState({ column: null, columnIndex: null, direction: "asc" }, "id", 0);
+ assert.deepEqual(asc, { column: "id", columnIndex: 0, direction: "asc" });
+
+ const desc = nextDataGridSortState(asc, "id", 0);
+ assert.deepEqual(desc, { column: "id", columnIndex: 0, direction: "desc" });
+
+ const cleared = nextDataGridSortState(desc, "id", 0);
+ assert.deepEqual(cleared, { column: null, columnIndex: null, direction: "asc" });
+});
+
+test("starts a new column at ascending sort", () => {
+ const next = nextDataGridSortState({ column: "id", columnIndex: 0, direction: "desc" }, "name", 1);
+
+ assert.deepEqual(next, { column: "name", columnIndex: 1, direction: "asc" });
+});