Added a function to generate the CVS file from the inputs selected by the user.

This commit is contained in:
Luis Aleixo 2022-08-05 16:28:24 +02:00
parent a83fcbeac3
commit e7bf7694f1
2 changed files with 103 additions and 0 deletions

View file

@ -860,4 +860,62 @@ function copy_clipboard(shareable_link) {
.tooltip('show');
navigator.clipboard.writeText(shareable_link);
}
function export_csv() {
```
This function generates a CSV file according to the user's input.
It is composed of a list of lists.
The first item of the main list corresponds to the columns' name.
The remaining items correspond to each of the file row, i.e. the
respective data from the selected inputs.
```
let final_export = [];
// Verify which items are checked
let export_lists = document.getElementsByName('checkedItems');
let checked_items = [];
let has_alternative_scenario = false;
export_lists.forEach(e => {
if (e.checked) {
if (e.id != "Alternative Scenarios") checked_items.push(e.id);
else if (e.id == "Alternative Scenarios") {
Object.entries(alternative_scenarios).map((scenario) => {
if (scenario[0] != 'Current scenario') {
checked_items.push(`Alternative scenario - ${scenario[0]}`);
has_alternative_scenario = true;
};
});
}
}
});
final_export.push(checked_items);
// Add the data for each column.
times.forEach((e, i) => {
let this_row = [];
checked_items.includes("Times") && this_row.push(times[i]);
checked_items.includes("Concentrations") && this_row.push(concentrations[i]);
checked_items.includes("Cumulative Dose") && this_row.push(cumulative_doses[i]);
checked_items.includes("Long-Range Dose") && this_row.push(long_range_cumulative_doses[i]);
if (has_alternative_scenario) {
Object.entries(alternative_scenarios).map((scenario) => {
if (scenario[0] != 'Current scenario') {
this_row.push(scenario[1].concentrations[i]);
};
});
};
final_export.push(this_row);
});
// Prepare the CSV file.
let csvContent = "data:text/csv;charset=utf8,"
+ final_export.map(e => e.join(",")).join("\n");
var encodedUri = encodeURI(csvContent);
// Set a name for the file.
var link = document.createElement("a");
link.setAttribute("href", encodedUri);
link.setAttribute("download", "report_data.csv");
document.body.appendChild(link);
link.click();
}

View file

@ -33,6 +33,51 @@
<button type="button" class="btn btn-outline-dark align-self-center" id="download-pdf" style="margin-right: -100pt" onclick="print();">Print Report</button>
<a href="{{ permalink.link }}" style="float: left;" id="pdf_qrcode_aref" class="align-self-center invisible mr-0"><div id="pdf_qrcode"></div></a>
</div>
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#modalCSV">Export CSV</button>
<!-- Modal -->
<div class="modal fade" id="modalCSV" tabindex="-1" role="dialog" aria-labelledby="modalCSV" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLongTitle">Export CSV Data</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
<div class="modal-body">
<b>Select the data to export:</b>
<div class="form-check mt-3">
<input type="checkbox" class="form-check-input" name="checkedItems" id="Times">
<label class="form-check-label" for="Times">Times of day</label>
</div>
<div class="form-check">
<input type="checkbox" class="form-check-input" name="checkedItems" id="Concentrations">
<label class="form-check-label" for="Concentrations">Concentrations</label>
</div>
<div class="form-check">
<input type="checkbox" class="form-check-input" name="checkedItems" id="Cumulative Dose">
<label class="form-check-label" for="Cumulative Dose">Cumulative dose</label>
</div>
{% if form.short_range_option == "short_range_yes" %}
<div class="form-check">
<input type="checkbox" class="form-check-input" name="checkedItems" id="Long-Range Dose">
<label class="form-check-label" for="Long-Range Dose">Long-range dose</label>
</div>
{% else %}
<div class="form-check">
<input type="checkbox" class="form-check-input" name="checkedItems" id="Alternative Scenarios">
<label class="form-check-label" for="Alternative Scenarios">Alternative Scenarios</label>
</div>
{% endif %}
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary" onClick="export_csv();">Export</button>
</div>
</div>
</div>
</div>
{% endblock report_header %}