Last active
March 19, 2024 10:25
-
-
Save jbPellicciaImensi/221c9822c6cefdd631398fd9a7a5b8ba to your computer and use it in GitHub Desktop.
Here is a little tricks to implement a download button in the Discovery tool of Warp10.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<title>File Download Example</title> | |
<script> | |
function downloadJSON(jsonData) { | |
fileName = "data.json"; | |
// Convert JSON object to a string and encode it to make it safe for URI. | |
const jsonString = encodeURIComponent(JSON.stringify(jsonData)); | |
// Create a data URI. | |
const dataUri = `data:application/json;charset=utf-8,${jsonString}`; | |
// Create a temporary anchor element and trigger download. | |
const a = document.createElement('a'); | |
a.href = dataUri; | |
a.download = fileName; | |
document.body.appendChild(a); // Append the anchor to the document. | |
a.click(); // Trigger the download. | |
document.body.removeChild(a); // Clean up and remove the anchor from the document. | |
} | |
</script> | |
<script nomodule src="https://unpkg.com/@senx/discovery-widgets/dist/discovery/discovery.js"></script> | |
<script type="module" src="https://unpkg.com/@senx/discovery-widgets/dist/discovery/discovery.esm.js"></script> | |
</head> | |
<body> | |
<discovery-dashboard url="https://sandbox.senx.io/api/v0/exec" dashboard-title="File Download Example"> | |
{ | |
'title' 'File Download Example' | |
'description' 'Download Example' | |
'tiles' [ | |
{ | |
'title' 'Download' | |
'type' 'display' | |
'x' 5 'y' 0 'w' 2 'h' 1 | |
'macro' <% | |
"<" "button style='background-color: DodgerBlue;' onclick='downloadJSON( {{my_json}} )' >Download</button>" | |
{ | |
"my_json" { "WARP" 7 3 + } ->JSON | |
} TEMPLATE | |
+ | |
%> | |
} | |
] | |
} | |
</discovery-dashboard> | |
<table id="table" style="border: 1px solid;" hidden></table> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment