fix(grid): refresh data after deleting rows
This commit is contained in:
@@ -101,9 +101,15 @@ export function useDataGridEditor(options: UseDataGridEditorOptions) {
|
||||
canEditExistingRows = computed(() => true),
|
||||
onExecuteSql,
|
||||
customSave,
|
||||
sql,
|
||||
searchText,
|
||||
whereFilterInput,
|
||||
orderByInput,
|
||||
rowStatusFilter,
|
||||
initialEditColumn,
|
||||
getRowItem,
|
||||
pageSize,
|
||||
currentPage,
|
||||
} = options;
|
||||
|
||||
const editingCell = ref<{ rowId: number; col: number } | null>(null);
|
||||
@@ -557,9 +563,22 @@ export function useDataGridEditor(options: UseDataGridEditorOptions) {
|
||||
});
|
||||
}
|
||||
|
||||
function reloadCurrentData() {
|
||||
options.emit(
|
||||
"reload",
|
||||
sql.value,
|
||||
searchText.value,
|
||||
whereFilterInput.value.trim() || undefined,
|
||||
orderByInput.value.trim() || undefined,
|
||||
pageSize.value,
|
||||
(currentPage.value - 1) * pageSize.value,
|
||||
);
|
||||
}
|
||||
|
||||
async function saveChanges() {
|
||||
saveError.value = "";
|
||||
isSaving.value = true;
|
||||
const shouldReloadAfterSave = deletedRows.value.size > 0;
|
||||
|
||||
if (customSave?.value) {
|
||||
try {
|
||||
@@ -580,6 +599,9 @@ export function useDataGridEditor(options: UseDataGridEditorOptions) {
|
||||
deletedRows.value.clear();
|
||||
exitTransaction();
|
||||
isSaving.value = false;
|
||||
if (shouldReloadAfterSave) {
|
||||
reloadCurrentData();
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -669,6 +691,9 @@ export function useDataGridEditor(options: UseDataGridEditorOptions) {
|
||||
deletedRows.value.clear();
|
||||
exitTransaction();
|
||||
isSaving.value = false;
|
||||
if (shouldReloadAfterSave) {
|
||||
reloadCurrentData();
|
||||
}
|
||||
}
|
||||
|
||||
function discardChanges() {
|
||||
|
||||
@@ -160,3 +160,113 @@ test("cloning a row clears auto-generated key columns", async () => {
|
||||
|
||||
assert.deepEqual(editor.newRows.value, [[null, "Ada"]]);
|
||||
});
|
||||
|
||||
test("saving deleted 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[][] = [];
|
||||
let editor: ReturnType<typeof useDataGridEditor>;
|
||||
|
||||
editor = useDataGridEditor({
|
||||
result,
|
||||
editable: computed(() => true),
|
||||
databaseType: computed(() => "postgres"),
|
||||
connectionId: computed(() => "conn-1"),
|
||||
database: computed(() => "main"),
|
||||
tableMeta: computed(() => ({
|
||||
tableName: "people",
|
||||
columns: [column("id", true), column("name")],
|
||||
primaryKeys: ["id"],
|
||||
})),
|
||||
onExecuteSql: computed(() => undefined),
|
||||
customSave: computed(() => async () => {}),
|
||||
sql: computed(() => "SELECT id, name FROM people"),
|
||||
searchText: ref("ada"),
|
||||
whereFilterInput: ref("name ILIKE '%a%'"),
|
||||
orderByInput: ref("id DESC"),
|
||||
rowStatusFilter,
|
||||
pageSize: ref(50),
|
||||
currentPage: ref(3),
|
||||
getRowItem: (rowId) => {
|
||||
if (rowId !== 0) return undefined;
|
||||
return {
|
||||
id: 0,
|
||||
sourceIndex: 0,
|
||||
data: result.value.rows[0],
|
||||
isNew: false,
|
||||
isDeleted: false,
|
||||
isDirtyCol: [false, false],
|
||||
status: "clean",
|
||||
};
|
||||
},
|
||||
emit: (...args) => {
|
||||
emitted.push(args);
|
||||
},
|
||||
});
|
||||
|
||||
editor.applyDeleteRow(0);
|
||||
await editor.saveChanges();
|
||||
|
||||
assert.deepEqual(emitted, [["reload", "SELECT id, name FROM people", "ada", "name ILIKE '%a%'", "id DESC", 50, 100]]);
|
||||
});
|
||||
|
||||
test("saving edited rows without deletes does not reload 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[][] = [];
|
||||
let editor: ReturnType<typeof useDataGridEditor>;
|
||||
|
||||
editor = useDataGridEditor({
|
||||
result,
|
||||
editable: computed(() => true),
|
||||
databaseType: computed(() => "postgres"),
|
||||
connectionId: computed(() => "conn-1"),
|
||||
database: computed(() => "main"),
|
||||
tableMeta: computed(() => ({
|
||||
tableName: "people",
|
||||
columns: [column("id", true), column("name")],
|
||||
primaryKeys: ["id"],
|
||||
})),
|
||||
onExecuteSql: computed(() => undefined),
|
||||
customSave: computed(() => async () => {}),
|
||||
sql: computed(() => "SELECT id, name FROM people"),
|
||||
searchText: ref(""),
|
||||
whereFilterInput: ref(""),
|
||||
orderByInput: ref(""),
|
||||
rowStatusFilter,
|
||||
pageSize: ref(50),
|
||||
currentPage: ref(1),
|
||||
getRowItem: (rowId) => {
|
||||
if (rowId !== 0) return undefined;
|
||||
return {
|
||||
id: 0,
|
||||
sourceIndex: 0,
|
||||
data: result.value.rows[0],
|
||||
isNew: false,
|
||||
isDeleted: false,
|
||||
isDirtyCol: [false, false],
|
||||
status: "clean",
|
||||
};
|
||||
},
|
||||
emit: (...args) => {
|
||||
emitted.push(args);
|
||||
},
|
||||
});
|
||||
|
||||
editor.applyCellValue(0, 1, "Ada Lovelace");
|
||||
await editor.saveChanges();
|
||||
|
||||
assert.deepEqual(emitted, []);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user