Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
compendium-v2
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
geant-swd
compendium-v2
Merge requests
!59
Draft: added csv download functionality to pages
Code
Review changes
Check out branch
Download
Patches
Plain diff
Closed
Draft: added csv download functionality to pages
add-csv-download-to-pages
into
develop
Overview
0
Commits
1
Pipelines
0
Changes
5
Closed
Mohammad Torkashvand
requested to merge
add-csv-download-to-pages
into
develop
1 year ago
Overview
0
Commits
1
Pipelines
0
Changes
5
Expand
0
0
Merge request reports
Compare
develop
version 1
48c65116
1 year ago
develop (base)
and
latest version
latest version
713e65ef
1 commit,
1 year ago
version 1
48c65116
1 commit,
1 year ago
5 files
+
53
−
18
Side-by-side
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
Files
5
Search (e.g. *.vue) (Ctrl+P)
compendium-frontend/src/components/DownloadCSVButton.tsx
0 → 100644
+
46
−
0
Options
import
React
from
'
react
'
;
interface
DownloadCSVProps
{
data
:
any
[];
filename
?:
string
;
}
function
convertToCSV
(
jsonData
:
any
[]):
string
{
if
(
!
jsonData
.
length
)
return
""
;
const
header
=
Object
.
keys
(
jsonData
[
0
]);
const
rows
=
jsonData
.
map
(
obj
=>
{
return
header
.
map
(
fieldName
=>
{
const
value
=
obj
[
fieldName
];
// Handle null values and wrap strings with commas or double-quotes in double quotes
return
value
===
null
?
''
:
typeof
value
===
'
string
'
&&
(
value
.
includes
(
'
,
'
)
||
value
.
includes
(
'
"
'
))
?
`"
${
value
.
replace
(
/"/g
,
'
""
'
)}
"`
:
value
;
}).
join
(
'
,
'
);
});
// Combine header and rows with newline characters
return
[
header
.
join
(
'
,
'
),
...
rows
].
join
(
'
\r\n
'
);
}
const
DownloadCSVButton
:
React
.
FC
<
DownloadCSVProps
>
=
({
data
,
filename
=
'
data.csv
'
})
=>
{
const
downloadCSV
=
()
=>
{
const
csv
=
convertToCSV
(
data
);
const
blob
=
new
Blob
([
csv
],
{
type
:
'
text/csv
'
});
const
link
=
document
.
createElement
(
'
a
'
);
link
.
href
=
URL
.
createObjectURL
(
blob
);
link
.
download
=
filename
;
document
.
body
.
appendChild
(
link
);
link
.
click
();
document
.
body
.
removeChild
(
link
);
}
return
(
<>
<
button
onClick
=
{
downloadCSV
}
>
Download CSV
</
button
>
</>
);
}
export
default
DownloadCSVButton
;
Loading