Skip to content

Commit 7c17a48

Browse files
committed
[Feat]: #1795 add table download
1 parent ba30103 commit 7c17a48

File tree

1 file changed

+59
-6
lines changed

1 file changed

+59
-6
lines changed

client/packages/lowcoder/src/comps/comps/tableComp/tableComp.tsx

Lines changed: 59 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,65 @@ export class TableImplComp extends TableInitComp implements IContainer {
9999
}
100100

101101
downloadData(fileName: string) {
102-
saveDataAsFile({
103-
data: (this as any).exposingValues["displayData"],
104-
filename: fileName,
105-
fileType: "csv",
106-
delimiter: this.children.toolbar.children.columnSeparator.getView(),
107-
});
102+
const allDisplayData = (this as any).exposingValues["displayData"];
103+
const delimiter = this.children.toolbar.children.columnSeparator.getView();
104+
105+
try {
106+
// Build the set of visible column keys as shown to the user (title or dataIndex)
107+
const enableColumnSetting = this.children.toolbar.children.columnSetting.getView();
108+
const visibleColumnKeys = new Set<string>();
109+
this.children.columns.getView().forEach((col) => {
110+
const colView = col.getView();
111+
const isHidden = columnHide({
112+
hide: colView.hide,
113+
tempHide: colView.tempHide,
114+
enableColumnSetting,
115+
});
116+
if (!isHidden) {
117+
const headerKey = (colView.title as any) || colView.dataIndex;
118+
if (headerKey) {
119+
visibleColumnKeys.add(String(headerKey));
120+
}
121+
}
122+
});
123+
124+
const pickVisible = (row: any): any => {
125+
const result: any = {};
126+
// copy only allowed keys
127+
Object.keys(row || {}).forEach((key) => {
128+
if (key !== COLUMN_CHILDREN_KEY && visibleColumnKeys.has(key)) {
129+
result[key] = row[key];
130+
}
131+
});
132+
// retain children recursively if present
133+
if (Array.isArray(row?.[COLUMN_CHILDREN_KEY])) {
134+
const children = row[COLUMN_CHILDREN_KEY].map((r: any) => pickVisible(r));
135+
if (children.length) {
136+
result[COLUMN_CHILDREN_KEY] = children;
137+
}
138+
}
139+
return result;
140+
};
141+
142+
const dataToSave = Array.isArray(allDisplayData)
143+
? allDisplayData.map((r: any) => pickVisible(r))
144+
: allDisplayData;
145+
146+
saveDataAsFile({
147+
data: dataToSave,
148+
filename: fileName,
149+
fileType: "csv",
150+
delimiter,
151+
});
152+
} catch (_e) {
153+
// Fallback to previous behavior if anything goes wrong
154+
saveDataAsFile({
155+
data: allDisplayData,
156+
filename: fileName,
157+
fileType: "csv",
158+
delimiter,
159+
});
160+
}
108161
}
109162

110163
refreshData(allQueryNames: Array<string>, setLoading: (loading: boolean) => void) {

0 commit comments

Comments
 (0)