fix(grid): reload after inserted rows save

This commit is contained in:
t8y2 2026-05-21 10:23:37 +08:00
parent f1a5beede5
commit ab6c73c687
2 changed files with 51 additions and 1 deletions

View File

@ -608,7 +608,7 @@ export function useDataGridEditor(options: UseDataGridEditorOptions) {
async function saveChanges() {
saveError.value = "";
isSaving.value = true;
const shouldReloadAfterSave = deletedRows.value.size > 0;
const shouldReloadAfterSave = newRows.value.length > 0 || deletedRows.value.size > 0;
if (customSave?.value) {
try {

View File

@ -216,6 +216,56 @@ test("saving deleted rows reloads current table data", async () => {
assert.deepEqual(emitted, [["reload", "SELECT id, name FROM people", "ada", "name ILIKE '%a%'", "id DESC", 50, 100]]);
});
test("saving inserted rows reloads current table data", async () => {
setActivePinia(createPinia());
installBrowserTestGlobals();
const result = computed(() => ({
columns: ["id", "name"],
rows: [[1, "Ada"] as CellValue[]],
}));
const rowStatusFilter = ref<"all" | "changed" | "edited" | "new" | "deleted">("all");
const emitted: unknown[][] = [];
const executedSql: string[] = [];
const editor = useDataGridEditor({
result,
editable: computed(() => true),
databaseType: computed(() => "postgres"),
connectionId: computed(() => undefined),
database: computed(() => undefined),
tableMeta: computed(() => ({
schema: "public",
tableName: "people",
columns: [column("id", true), column("name")],
primaryKeys: ["id"],
})),
onExecuteSql: computed(() => async (sql: string) => {
executedSql.push(sql);
}),
customSave: computed(() => undefined),
sql: computed(() => "SELECT id, name FROM people"),
searchText: ref("linus"),
whereFilterInput: ref("name ILIKE '%l%'"),
orderByInput: ref("id DESC"),
rowStatusFilter,
pageSize: ref(50),
currentPage: ref(2),
getRowItem: () => undefined,
emit: (...args) => {
emitted.push(args);
},
});
editor.newRows.value = [[2, "Linus"]];
await editor.saveChanges();
assert.deepEqual(executedSql, [`INSERT INTO "public"."people" ("id", "name") VALUES (2, 'Linus');`]);
assert.deepEqual(emitted, [
["reload", "SELECT id, name FROM people", "linus", "name ILIKE '%l%'", "id DESC", 50, 50],
]);
});
test("saving edited rows without deletes does not reload table data", async () => {
setActivePinia(createPinia());
installBrowserTestGlobals();