Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions src/components/renderer/form-record-list.vue
Original file line number Diff line number Diff line change
Expand Up @@ -739,6 +739,83 @@ export default {
});
//sets Collection result(columns and rows) into this.collectionData
this.collectionData = result;
this.reapplyCollectionSelections(result);
},
// Keep selected rows in sync after collection refreshes triggered by PMQL filters.
reapplyCollectionSelections(newCollection) {
if (!this.shouldPersistCollectionSelection()) {
return;
}

if (!Array.isArray(this.selectedRows) || this.selectedRows.length === 0) {
return;
}

const keyCounts = this.selectedRows.reduce((accumulator, item) => {
const key = this.getCollectionRowKey(item);
if (!key) {
return accumulator;
}
const occurrences = accumulator.get(key) || 0;
accumulator.set(key, occurrences + 1);
return accumulator;
}, new Map());

if (keyCounts.size === 0) {
this.selectedRows = [];
return;
}

const updatedSelection = [];

newCollection.forEach((row, index) => {
const rowKey = this.getCollectionRowKey(row);
const occurrences = keyCounts.get(rowKey);
if (!occurrences) {
return;
}

// eslint-disable-next-line no-param-reassign
row.selectedRowsIndex = index;
updatedSelection.push(row);

if (occurrences === 1) {
keyCounts.delete(rowKey);
} else {
keyCounts.set(rowKey, occurrences - 1);
}
});

this.selectedRows = updatedSelection;
},
shouldPersistCollectionSelection() {
const pmql = this.source?.collectionFields?.pmql;
return (
this.source?.sourceOptions === "Collection" &&
this.source?.dataSelectionOptions === "multiple-records" &&
typeof pmql === "string" &&
pmql.trim().length > 0
);
},
getCollectionRowKey(item) {
if (!item || typeof item !== "object") {
return null;
}

const entries = Object.entries(item).filter(
([key]) => key !== "selectedRowsIndex"
);

if (entries.length === 0) {
return null;
}

entries.sort(([keyA], [keyB]) => {
if (keyA > keyB) return 1;
if (keyA < keyB) return -1;
return 0;
});
return JSON.stringify(entries);
},
updateRowDataNamePrefix() {
this.setUploadDataNamePrefix(this.currentRowIndex);
Expand Down
Loading