Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Button for exporting tables as csv #429

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
14 changes: 13 additions & 1 deletion puppetboard/templates/_macros.html
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,19 @@
"search": {"regex": true},
// Default sort
"order": [[ 0, "desc" ]],
// Custom options
// Rendering - add rendering options for columns
"columnDefs": [ {
"targets": -1,
"data:": null,
"render": function (data, type, full, meta) {
shorta = data.replace(/[{},]/g, "<br />");
shortb = shorta.replace(/u'/g, " ");
shortc = shortb.replace(/'/g, " ");
return shortc;
}}],
// Custom options
{% if extra_options %}{% call extra_options() %}Callback to parent defined options{% endcall %}{% endif %}
});
{% if extra_options %}{% call extra_options() %}Callback to parent defined options{% endcall %}{% endif %}
});

Expand Down
Binary file added puppetboard/templates/favicon.ico
Binary file not shown.
59 changes: 59 additions & 0 deletions puppetboard/templates/layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{{config.PAGE_TITLE}}</title>
<button id="export_data_button" style="clear: right;width:127px;height:50px;color:#0a214a;box-shadow: 4px 4px #0a214a;margin-top: 5 px; margin-left: 5 px">Export data as csv</button>
{% if config.OFFLINE_MODE %}
<style>
@font-face {
Expand Down Expand Up @@ -56,6 +57,64 @@
$.getScript('{{url_for('static', filename='js/tables.js')}}')
{% block onload_script %} {% endblock onload_script %}
})

$('#export_data_button').click(function() {
var titles = [];
var data = [];

$('.dataTable th').each(function() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The table doesn't have this class name on any of the pages I'm looking at.

What page did you test this on?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok I see it works perfect on the Inventory page.

Do you only intend to use it with inventory? Or do you want it to work on other pages too?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it works for all pages. I have tested on all facts, and it works there too

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the browser? I have tested with IE and sometimes it doesn't work there

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems to work on all fact pages for me too.

It doesn't work on Overview, Nodes, Facts, Reports, Metrics, Catalogs (kind of works), or Query.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is because the data in Nodes,Facts ( try opening a fact, e.g. "ip adresses" there it works), reports is not initialized from dataTable, so there are no headers and data cells. So this code will only work on any data from dataTable

titles.push($(this).text());
});

$('.dataTable td').each(function() {
data.push($(this).text());
});

var CSVString = prepCSVRow(titles, titles.length, '');
CSVString = prepCSVRow(data, titles.length, CSVString);

var downloadLink = document.createElement("a");
var blob = new Blob(["\ufeff", CSVString]);
var url = URL.createObjectURL(blob);
downloadLink.href = url;
downloadLink.download = "puppetboard_data.csv";

document.body.appendChild(downloadLink);
downloadLink.click();
document.body.removeChild(downloadLink);
});

function prepCSVRow(arr, columnCount, initial) {
var row = '';
var delimeter = ',';
var newLine = '\r\n';

function splitArray(_arr, _count) {
var splitted = [];
var result = [];
_arr.forEach(function(item, idx) {
if ((idx + 1) % _count === 0) {
splitted.push(item);
result.push(splitted);
splitted = [];
} else {
splitted.push(item);
}
});
return result;
}

var plainArr = splitArray(arr, columnCount);

plainArr.forEach(function(arrItem) {
arrItem.forEach(function(item, idx) {
row += item + ((idx + 1) === arrItem.length ? '' : delimeter);
});
row += newLine;
});
return initial + row;
};

</script>
{% block head %} {% endblock head %}
</head>
Expand Down