fix(views/table): position not restored after new columns (closes #6285)

This commit is contained in:
Elian Doran
2025-07-14 20:32:24 +03:00
parent 2d4ac93221
commit 960d321019
2 changed files with 52 additions and 3 deletions

View File

@@ -0,0 +1,49 @@
import { describe, expect, it } from "vitest";
import { restoreExistingData } from "./columns";
import type { ColumnDefinition } from "tabulator-tables";
describe("restoreExistingData", () => {
it("should restore existing column data", () => {
const newDefs: ColumnDefinition[] = [
{ field: "title", title: "Title", editor: "input" },
{ field: "noteId", title: "Note ID", visible: false }
];
const oldDefs: ColumnDefinition[] = [
{ field: "title", title: "Title", width: 300, visible: true },
{ field: "noteId", title: "Note ID", width: 200, visible: true }
];
const restored = restoreExistingData(newDefs, oldDefs);
expect(restored[0].width).toBe(300);
expect(restored[1].width).toBe(200);
});
it("restores order of columns", () => {
const newDefs: ColumnDefinition[] = [
{ field: "title", title: "Title", editor: "input" },
{ field: "noteId", title: "Note ID", visible: false }
];
const oldDefs: ColumnDefinition[] = [
{ field: "noteId", title: "Note ID", width: 200, visible: true },
{ field: "title", title: "Title", width: 300, visible: true }
];
const restored = restoreExistingData(newDefs, oldDefs);
expect(restored[0].field).toBe("noteId");
expect(restored[1].field).toBe("title");
});
it("inserts new columns at given position", () => {
const newDefs: ColumnDefinition[] = [
{ field: "title", title: "Title", editor: "input" },
{ field: "noteId", title: "Note ID", visible: false },
{ field: "newColumn", title: "New Column", editor: "input" }
];
const oldDefs: ColumnDefinition[] = [
{ field: "title", title: "Title", width: 300, visible: true },
{ field: "noteId", title: "Note ID", width: 200, visible: true }
];
const restored = restoreExistingData(newDefs, oldDefs, 0);
expect(restored[0].field).toBe("newColumn");
expect(restored[1].field).toBe("title");
expect(restored[2].field).toBe("noteId");
});
});

View File

@@ -92,14 +92,15 @@ export function buildColumnDefinitions(info: AttributeDefinitionInformation[], m
return columnDefs;
}
function restoreExistingData(newDefs: ColumnDefinition[], oldDefs: ColumnDefinition[], position?: number) {
export function restoreExistingData(newDefs: ColumnDefinition[], oldDefs: ColumnDefinition[], position?: number) {
const existingColumns: ColumnDefinition[] = []
const byField = new Map<string, ColumnDefinition>;
for (const def of oldDefs) {
byField.set(def.field ?? "", def);
existingColumns.push(def);
}
const newColumns: ColumnDefinition[] = [];
const existingColumns: ColumnDefinition[] = []
for (const newDef of newDefs) {
const oldDef = byField.get(newDef.field ?? "");
if (!oldDef) {
@@ -107,7 +108,6 @@ function restoreExistingData(newDefs: ColumnDefinition[], oldDefs: ColumnDefinit
} else {
newDef.width = oldDef.width;
newDef.visible = oldDef.visible;
existingColumns.push(newDef);
}
}