Skip to content

Commit 6a81dfe

Browse files
frouenepcdavid
authored andcommitted
[5699] Preserve column order and visibility during table data CSV export
Bug: #5699 Signed-off-by: Florian ROUËNÉ <[email protected]>
1 parent 779e03d commit 6a81dfe

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

CHANGELOG.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ Use `org.eclipse.emf.ecore.util.ECrossReferenceAdapter.getCrossReferenceAdapter(
134134
- https://github.com/eclipse-sirius/sirius-web/issues/5598[#5598] [diagram] Fix a performance issue with how label where rendered when dragging the viewport
135135
- https://github.com/eclipse-sirius/sirius-web/issues/5797[#5797] [sirius-web] Make the _New Project_ command use a Sirius Web icon instead of a Papaya icon
136136
The command has also been renamed _Blank project_ to reflect what it currently does (template and blank project creation haven't been unified yet).
137-
137+
- https://github.com/eclipse-sirius/sirius-web/issues/5699[#5699] [table] Preserve column order and visibility during CSV export
138138

139139
=== New Features
140140

packages/tables/frontend/sirius-components-tables/src/actions/ExportDataButton.tsx

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,21 @@ const csvConfig = mkConfig({
5757
useKeysAsHeaders: true,
5858
});
5959

60-
export const handleExportData = (table: GQLTable, getCellLabel: (cell: GQLCell) => string) => {
60+
const handleExportData = (table: GQLTable) => {
61+
const visibleColumns = table.columns.filter((column) => !column.hidden);
62+
const visibleColumnIds = new Set(visibleColumns.map((col) => col.id));
63+
const columnOrder = {};
64+
visibleColumns.forEach((column, index) => (columnOrder[column.id] = index));
65+
6166
const columnIdToLabel = {};
62-
table.columns.forEach((column) => (columnIdToLabel[column.id] = column.headerLabel));
67+
visibleColumns.forEach((column) => (columnIdToLabel[column.id] = column.headerLabel));
6368

6469
const csvData: CsvData[] = table.lines.map((line) => {
6570
const csvDatum: CsvData = { ['_row_header_']: `${line.headerIndexLabel} ${line.headerLabel}` };
66-
line.cells.forEach((cell) => (csvDatum[columnIdToLabel[cell.columnId]] = getCellLabel(cell)));
71+
const sortedVisibleCells = [...line.cells]
72+
.filter((cell) => visibleColumnIds.has(cell.columnId))
73+
.sort((a, b) => columnOrder[a.columnId] - columnOrder[b.columnId]);
74+
sortedVisibleCells.forEach((cell) => (csvDatum[columnIdToLabel[cell.columnId]] = getCellLabel(cell)));
6775
return csvDatum;
6876
});
6977

@@ -73,7 +81,7 @@ export const handleExportData = (table: GQLTable, getCellLabel: (cell: GQLCell)
7381

7482
export const ExportDataButton = ({ table }: ExportDataButtonProps) => {
7583
return (
76-
<MenuItem onClick={() => handleExportData(table, getCellLabel)}>
84+
<MenuItem onClick={() => handleExportData(table)}>
7785
<ListItemIcon>
7886
<FileDownloadIcon fontSize="small" />
7987
</ListItemIcon>

0 commit comments

Comments
 (0)