This commit is contained in:
parent
67f8d718e8
commit
79e87833b9
|
@ -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) => {
|
||||
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 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 });
|
||||
},
|
||||
{ deep: true },
|
||||
);
|
||||
}
|
||||
async function reset() {
|
||||
await fetch(originalData.value);
|
||||
|
@ -185,7 +191,6 @@ async function onSubmit() {
|
|||
isLoading.value = true;
|
||||
|
||||
await saveChanges($props.saveFn ? formData.value : null);
|
||||
|
||||
}
|
||||
|
||||
async function onSubmitAndGo() {
|
||||
|
@ -194,8 +199,8 @@ 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) {
|
||||
|
@ -228,7 +233,7 @@ 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;
|
||||
|
@ -239,20 +244,18 @@ async function insert(pushData = { ...$props.dataRequired, ...$props.dataDefault
|
|||
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) {
|
||||
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();
|
||||
});
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue