From 79e87833b9e16b0c28d29ec577fd2914b6177560 Mon Sep 17 00:00:00 2001 From: jorgep Date: Thu, 15 May 2025 13:29:40 +0200 Subject: [PATCH] fix: refs #8388 reload on delete --- src/components/CrudModel.vue | 57 ++++++++++++++++++++---------------- 1 file changed, 31 insertions(+), 26 deletions(-) diff --git a/src/components/CrudModel.vue b/src/components/CrudModel.vue index 21ad0e41a..4128fa44c 100644 --- a/src/components/CrudModel.vue +++ b/src/components/CrudModel.vue @@ -131,11 +131,10 @@ async function fetch(data) { const rows = keyData ? data[keyData] : data; resetData(rows); emit('onFetch', rows); - $props.insertOnLoad && await insert(); + $props.insertOnLoad && (await insert()); return rows; } - function resetData(data) { if (!data) return; if (data && Array.isArray(data)) { @@ -146,15 +145,22 @@ function resetData(data) { formData.value = JSON.parse(JSON.stringify(data)); if (watchChanges.value) watchChanges.value(); //destroy watcher - watchChanges.value = watch(formData, (nVal) => { - hasChanges.value = false; - const filteredNewData = nVal.filter(row => !isRowEmpty(row) || row[$props.primaryKey]); - const filteredOriginal = originalData.value.filter(row => row[$props.primaryKey]); + watchChanges.value = watch( + formData, + (nVal) => { + hasChanges.value = false; + const filteredNewData = nVal.filter( + (row) => !isRowEmpty(row) || row[$props.primaryKey], + ); + const filteredOriginal = originalData.value.filter( + (row) => row[$props.primaryKey], + ); - const changes = getDifferences(filteredOriginal, filteredNewData); - hasChanges.value = !isEmpty(changes); - - }, { deep: true }); + const changes = getDifferences(filteredOriginal, filteredNewData); + hasChanges.value = !isEmpty(changes); + }, + { deep: true }, + ); } async function reset() { await fetch(originalData.value); @@ -183,9 +189,8 @@ async function onSubmit() { }); } isLoading.value = true; - + await saveChanges($props.saveFn ? formData.value : null); - } async function onSubmitAndGo() { @@ -194,10 +199,10 @@ async function onSubmitAndGo() { } async function saveChanges(data) { - formData.value = formData.value.filter(row => - row[$props.primaryKey] || !isRowEmpty(row) + formData.value = formData.value.filter( + (row) => row[$props.primaryKey] || !isRowEmpty(row), ); - + if ($props.saveFn) { $props.saveFn(data, getChanges); isLoading.value = false; @@ -228,31 +233,29 @@ async function saveChanges(data) { } async function insert(pushData = { ...$props.dataRequired, ...$props.dataDefault }) { - formData.value = formData.value.filter(row => !isRowEmpty(row)); + formData.value = formData.value.filter((row) => !isRowEmpty(row)); const lastRow = formData.value.at(-1); const isLastRowEmpty = lastRow ? isRowEmpty(lastRow) : false; - + if (formData.value.length && isLastRowEmpty) return; const $index = formData.value.length ? formData.value.at(-1).$index + 1 : 0; - + const nRow = Object.assign({ $index }, pushData); formData.value.push(nRow); - - const hasChange = Object.keys(nRow).some(key => !isChange(nRow, key)); + + const hasChange = Object.keys(nRow).some((key) => !isChange(nRow, key)); if (hasChange) hasChanges.value = true; } function isRowEmpty(row) { - return Object.keys(row).every(key => isChange(row, key)); + return Object.keys(row).every((key) => isChange(row, key)); } - -function isChange(row,key){ +function isChange(row, key) { return !row[key] || key == '$index' || Object.hasOwn($props.dataRequired || {}, key); } - async function remove(data) { if (!data.length) return quasar.notify({ @@ -270,7 +273,9 @@ async function remove(data) { (form) => !preRemove.some((index) => index == form.$index), ); formData.value = newData; - hasChanges.value = JSON.stringify(removeIndexField(formData.value)) !== JSON.stringify(removeIndexField(originalData.value)); + hasChanges.value = + JSON.stringify(removeIndexField(formData.value)) !== + JSON.stringify(removeIndexField(originalData.value)); } if (ids.length) { quasar @@ -286,7 +291,7 @@ async function remove(data) { }) .onOk(async () => { newData = newData.filter((form) => !ids.some((id) => id == form[pk])); - fetch(newData); + await reload(); }); }