Skip to content

Commit f03e09f

Browse files
authored
Merge pull request #17 from nuonco/fd/render-nested-data-as-json-string
2 parents 7fa9f39 + dd58359 commit f03e09f

File tree

3 files changed

+40
-2
lines changed

3 files changed

+40
-2
lines changed

src/TabContents/QueryTabContent.jsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import {
2525
PopoverTrigger,
2626
} from "@/components/ui/popover";
2727
import downloadCsv from "/src/helpers/donwloadCsv.js";
28+
import transformRows from "/src/helpers/transformRows.js";
2829
import { suggestions } from "@/providers/AutoCompleteMonaco";
2930

3031
export default function QueryTabContent({ tab }) {
@@ -237,7 +238,7 @@ export default function QueryTabContent({ tab }) {
237238
) : (
238239
<AgGridReact
239240
alwaysShowVerticalScroll={true}
240-
rowData={tab.tab_results}
241+
rowData={transformRows(tab.tab_results)}
241242
enableCellTextSelection={true}
242243
columnDefs={
243244
tab.tab_results && tab.tab_results.length > 0

src/TabContents/TableTabContent.jsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { AgGridReact } from "ag-grid-react"; // React Data Grid Component
55
import "ag-grid-community/styles/ag-grid.css"; // Mandatory CSS required by the grid
66
import "ag-grid-community/styles/ag-theme-alpine.css"; // Optional theme CSS
77
import downloadCsv from "/src/helpers/donwloadCsv.js";
8+
import transformRows from "/src/helpers/transformRows.js";
89

910
import {
1011
Popover,
@@ -251,7 +252,7 @@ export default function TableTabContent({ tab }) {
251252
<AgGridReact
252253
enableCellTextSelection={true}
253254
alwaysShowVerticalScroll={true}
254-
rowData={currentPreview}
255+
rowData={transformRows(currentPreview)}
255256
columnDefs={Object.keys(currentPreview[0])?.map((key) => ({
256257
headerName: key,
257258
field: key,

src/helpers/transformRows.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
const transformRows = (previewData) => {
2+
if (!previewData) {
3+
return previewData;
4+
}
5+
6+
let data = previewData.map((row) => {
7+
let newRow = {};
8+
Object.keys(row).forEach((key) => {
9+
let value = row[key];
10+
// type checks
11+
if (
12+
typeof value === "object" &&
13+
!Array.isArray(value) &&
14+
value !== null
15+
) {
16+
newRow[key] = JSON.stringify(value);
17+
} else if (Array.isArray(value)) {
18+
// consider recursively checking the children
19+
let value = row[key];
20+
newRow[key] = value.map((el) => {
21+
if (typeof el === "object" && !Array.isArray(el) && el !== null) {
22+
return JSON.stringify(el);
23+
} else {
24+
return el;
25+
}
26+
});
27+
} else {
28+
newRow[key] = row[key];
29+
}
30+
});
31+
return newRow;
32+
});
33+
return data;
34+
};
35+
36+
export default transformRows;

0 commit comments

Comments
 (0)