Skip to content

Commit

Permalink
issue ubccr#222 : downloads the filtered data
Browse files Browse the repository at this point in the history
  • Loading branch information
bhatiadheeraj committed Nov 12, 2020
1 parent 5552b17 commit 19b1b3f
Showing 1 changed file with 49 additions and 3 deletions.
52 changes: 49 additions & 3 deletions coldfront/core/grant/templates/grant/grant_report_list.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<div class="card">
<div class="card-header">
<h4 class="d-inline"><i class="fas fa-trophy" aria-hidden="true"></i> Grants</h4>
<a class="btn btn-success float-right" id='Export_BTN' href="{% url 'grant-download'%}" role="button"><i class="fas fa-download" aria-hidden="true"></i> Export to CSV</a>
<a class="btn btn-success float-right" id='Export_BTN' role="button"><i class="fas fa-download" aria-hidden="true"></i> Export to CSV</a>
</div>
<div class="card-body">
{% if grant_list %}
Expand Down Expand Up @@ -81,6 +81,7 @@ <h4 class="d-inline"><i class="fas fa-trophy" aria-hidden="true"></i> Grants</h4
//gets rows of table
var rowLength = oTable.rows.length;
var grant_title = [];

//loops through rows
for (i = 1; i < rowLength; i++){

Expand All @@ -93,16 +94,61 @@ <h4 class="d-inline"><i class="fas fa-trophy" aria-hidden="true"></i> Grants</h4
//loops through each cell in current row
for(var j = 0; j < cellLength; j++){
/* get your cell info here */
var cellVal = oCells.item(0).innerHTML;
var cellVal = oCells.item(j).innerHTML;
console.log(cellVal);
grant_title.push(cellVal);
}
}

console.log('here');
console.log(grant_title);
var Export_BTN= document.getElementById('Export_BTN');
Export_BTN.onclick = function(){
var html = document.querySelector("table").outerHTML;
var filename = 'grants.csv'
var csv = [];
var rows = $("table tr:visible");

for (var i = 0; i < rows.length; i++) {
var row = [], cols = rows[i].querySelectorAll("td, th");

for (var j = 0; j < cols.length; j++)
row.push(""+cols[j].innerText);

csv.push(row.join(","));
}

// Download CSV
download_csv(csv.join("\n"), filename);
};
});
});

function download_csv(csv, filename) {
var csvFile;
var downloadLink;

// CSV FILE
csvFile = new Blob([csv], {type: "text/csv"});

// Download link
downloadLink = document.createElement("a");

// File name
downloadLink.download = filename;

// We have to create a link to the file
downloadLink.href = window.URL.createObjectURL(csvFile);

// Make sure that the link is not displayed
downloadLink.style.display = "none";

// Add the link to your DOM
document.body.appendChild(downloadLink);

// Lanzamos
downloadLink.click();
}

function setCook(name, value) {
var cookie = [
name,
Expand Down

0 comments on commit 19b1b3f

Please sign in to comment.